本篇分享简单介绍C#中的Http请求,前几天帮朋友的项目封装ApiHelper,我粗糙的结果就如下,想想我真的是差的太远了。还有一位架构师也在封装这个Helper , 所以最后的结果当然是使用大牛的封装,这篇分享后,准备学习下他的封装,配置,错误处理机制等思想。不过这次也使我对C#网络编程了解的更多,算是一次学习经历吧。真心喜欢和老司机合作,下一阶段将会持续跟这位朋友学习项目底层的封装,和他谦虚的态度,有这样的机会也是幸运的。
你可以将其封装成自己的HttpHelper,经朋友建议,统一Http请求的入参和出参。在HttpClient方面也参考了dudu的关于httpclient预热的文章。C#中HttpClient使用注意:预热与长连接。
为了做到统一入参和出参,定义了Req
public class Req{ /// /// 传入数据 /// public T Input { get; set; } ////// 请求地址 /// public string Url { get; set; } }
public class Resp{ /// /// 错误消息 /// public string ErrorMsg { get; set; } ////// 状态码 /// public int StatusCode { get; set; } ////// 返回数据 /// public T RespData { get; set; } }
虽然保持了httpClient对象复用,但需要注意的是,在设置了httpClient,并且发生了一次请求之后,不能再对其属性进行重新设置。这也正是我又定义了一个fileClient的理由。
#region HttpClient版本 private static readonly string _baseAddress = ConfigurationManager.AppSettings["api-server"];//配置BaseUrl eg.http://localhost:1234 private static readonly HttpClient _httpClient; private static readonly HttpClient _fileClient; static ApiHelper() { #region 初始化和预热 httpClient _httpClient = new HttpClient(); _httpClient.BaseAddress = new Uri(_baseAddress); _httpClient.Timeout = TimeSpan.FromMilliseconds(2000); _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");//application/xml 想Accept的数据格式 _httpClient.SendAsync(new HttpRequestMessage { Method = new HttpMethod("HEAD"), RequestUri = new Uri(_baseAddress + "/api/test/HttpClientHot") }) .Result.EnsureSuccessStatusCode(); #endregion #region 初始化和预热 fileClient _fileClient = new HttpClient(); _fileClient.BaseAddress = new Uri(_baseAddress + "/api/test/HttpClientHot"); _fileClient.MaxResponseContentBufferSize = 256000; #endregion } ////// http Get请求 /// ///入参类型 ///出参类型 /// 入参对象 ///public static async Task > GetAsync (Req req) { try var result =await _httpClient.GetAsync(req.Url).Result.Content.ReadAsStringAsync(); return new Resp () { Data = JsonHelper.JsonDeserialize (result) }; } catch(Exception ex) } return new Resp () { Data = JsonHelper.JsonDeserialize ("") }; } /// /// http Post请求 /// ///入参类型 ///出参类型 /// 入参对象 ///public static async Task > PostAsJsonAsync (Req req) var result = await _httpClient.PostAsJsonAsync(req.Url, req.Input).Result.Content.ReadAsStringAsync(); return new Resp () { Data = JsonHelper.JsonDeserialize (result) }; } /// 上传文件 /// /// /// /// public static async Task > SendFile (Req req, string filePath)//D:\\white.jpg //_fileClient.DefaultRequestHeaders.Add("user-agent", "User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; MALNJS; rv:11.0) like Gecko");//设置请求头 // 读文件流 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); HttpContent fileContent = new StreamContent(fs);//为文件流提供的HTTP容器 fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");//设置媒体类型 MultipartFormDataContent mulContent = new MultipartFormDataContent("----");//创建用于可传递文件的容器 string fileName = filePath.Substring(filePath.LastIndexOf("/") + 1); mulContent.Add(fileContent, "form", fileName);//第二个参数是表单名,第三个是文件名。 HttpResponseMessage response = await _fileClient.PostAsync(req.Url, mulContent); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); return new Resp () { Data = JsonHelper.JsonDeserialize (result) }; } /// 下载 /// /// public static async Task > HttpDownloadData (Req req) var byteres = await _fileClient.GetByteArrayAsync(req.Url); return new Resp () { Data = byteres }; #endregion }
另外分享下HttpWebRequest来实现的请求。HttpWebRequest需要你自行设置很多内容,当然这也证明其内容丰富。下面代码包含了post,get,以及上传。
////// Post Http请求 /// /// 请求地址 /// 传输数据 /// 超时时间 /// 媒体格式 /// 编码 ///泛型集合 public static ListPostAndRespList (string url, string postData, int timeout = 5000, string contentType = "application/json;", string encode = "UTF-8") { if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null) { // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData"); HttpWebResponse webResponse = null; Stream responseStream = null; Stream requestStream = null; StreamReader streamReader = null; try { string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType); return JsonHelper.JsonDeserialize >(respstr); } catch (Exception ex) { } finally { if (responseStream != null) responseStream.Dispose(); if (webResponse != null) webResponse.Dispose(); if (requestStream != null) requestStream.Dispose(); if (streamReader != null) streamReader.Dispose(); } } return null; } ///
/// Post Http请求 /// /// 请求地址 /// 传输数据 /// 超时时间 /// 媒体格式 /// 编码 ///泛型集合 public static T PostAndRespSignle(string url, int timeout = 5000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") { if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null) { // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData"); HttpWebResponse webResponse = null; Stream responseStream = null; Stream requestStream = null; StreamReader streamReader = null; try { string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType); return JsonHelper.JsonDeserialize (respstr); } catch (Exception ex) { } finally { if (responseStream != null) responseStream.Dispose(); if (webResponse != null) webResponse.Dispose(); if (requestStream != null) requestStream.Dispose(); if (streamReader != null) streamReader.Dispose(); } } return default(T); } /// /// Post Http请求 /// /// /// /// /// /// ///响应流字符串 public static string PostAndRespStr(string url, int timeout = 5000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") { if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null) { HttpWebResponse webResponse = null; Stream responseStream = null; Stream requestStream = null; StreamReader streamReader = null; try { return GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType); } catch (Exception ex) { } finally { if (responseStream != null) responseStream.Dispose(); if (webResponse != null) webResponse.Dispose(); if (requestStream != null) requestStream.Dispose(); if (streamReader != null) streamReader.Dispose(); } } return null; } private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType) { byte[] data = Encoding.GetEncoding(encode).GetBytes(postData); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); SetAuth(webRequest); webRequest.Method = "POST"; webRequest.ContentType = contentType + ";" + encode; webRequest.ContentLength = data.Length; webRequest.Timeout = timeout; requestStream = webRequest.GetRequestStream(); requestStream.Write(data, 0, data.Length); webResponse = (HttpWebResponse)webRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); if (responseStream == null) { return ""; } streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode)); return streamReader.ReadToEnd(); } ////// Post文件流给指定Url /// /// url /// 文件路径 ///响应流字符串 public static string PostFile(string url, string filePath, string contentType = "application/octet-stream", string encode = "UTF-8") { if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(filePath)) { Stream requestStream = null; Stream responseStream = null; StreamReader streamReader = null; FileStream fileStream = null; try { // 设置参数 HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest; SetAuth(webRequest); webRequest.AllowAutoRedirect = true; webRequest.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 webRequest.ContentType = "multipart/form-data;charset=" + encode + ";boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");//消息开始 byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");//消息结尾 var fileName = filePath.Substring(filePath.LastIndexOf("/") + 1); //请求头部信息 string postHeader = string.Format("Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:{1}\r\n\r\n", fileName, contentType); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); byte[] fileByteArr = new byte[fileStream.Length]; fileStream.Read(fileByteArr, 0, fileByteArr.Length); fileStream.Close(); requestStream = webRequest.GetRequestStream(); requestStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); requestStream.Write(fileByteArr, 0, fileByteArr.Length); requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); requestStream.Close(); responseStream = webRequest.GetResponse().GetResponseStream();//发送请求,得到响应流 if (responseStream == null) return string.Empty; streamReader = new StreamReader(responseStream, Encoding.UTF8); return streamReader.ReadToEnd(); } catch (Exception ex) { } finally { } } return null; } ////// Get http请求 /// /// 请求地址 /// 超时时间 /// 编码 ///返回单个实体 public static T GetSingle(string url, int timeout = 5000, string encode = "UTF-8") { //HttpWebRequest对象 //HttpClient->dudu 调用预热处理 //Stream—>Model if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode)) { Stream responseStream = null; StreamReader streamReader = null; WebResponse webResponse = null; try { string respStr = GetRespStr(url, responseStream, streamReader, webResponse, timeout, encode); return JsonHelper.JsonDeserialize (respStr); } catch (Exception ex) { } finally { if (responseStream != null) responseStream.Dispose(); if (streamReader != null) streamReader.Dispose(); if (webResponse != null) webResponse.Dispose(); } } return default(T); } /// /// Get http请求 /// /// /// /// ///响应流字符串 public static string GetResponseString(string url, int timeout = 5000, string encode = "UTF-8") { if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode)) { Stream responseStream = null; StreamReader streamReader = null; WebResponse webResponse = null; try { return GetRespStr(url, responseStream, streamReader, webResponse, timeout, encode); } catch (Exception ex) { } finally { if (responseStream != null) responseStream.Dispose(); if (streamReader != null) streamReader.Dispose(); if (webResponse != null) webResponse.Dispose(); } } return null; } private static string GetRespStr(string url, Stream responseStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Method = "GET"; webRequest.Timeout = timeout; webResponse = webRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); if (responseStream == null) { return ""; } streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode)); return streamReader.ReadToEnd(); }
到此这篇关于C#网络编程中的Http请求的文章就介绍到这了,更多相关C#网络编程Http请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!