[关闭]
@Aiti 2019-11-19T01:40:44.000000Z 字数 2832 阅读 202

C# 后台请求Http

Http请求


  1. public static class HttpHelper
  2. {
  3. /// <summary>
  4. /// 获取返回结果http请求
  5. /// </summary>
  6. /// <param name="webRequest">webRequest对象</param>
  7. /// <returns>请求返回值</returns>
  8. public static string WebResponseGet(HttpWebRequest webRequest)
  9. {
  10. try
  11. {
  12. using (var httpWebResponse = (HttpWebResponse)webRequest.GetResponseAsync().Result)
  13. {
  14. var stream = httpWebResponse.GetResponseStream();
  15. if (stream == null) return null;
  16. using (var responseReader = new StreamReader(stream, Encoding.UTF8))
  17. {
  18. return responseReader.ReadToEnd();
  19. }
  20. }
  21. }
  22. catch (WebException wex)
  23. {
  24. var responseData = String.Empty;
  25. if (wex.Status == WebExceptionStatus.ProtocolError)
  26. {
  27. try
  28. {
  29. var wexStream = wex.Response.GetResponseStream();
  30. if (wexStream == null) return null;
  31. using (StreamReader responseReader = new StreamReader(wexStream))
  32. {
  33. responseData = responseReader.ReadToEnd();
  34. }
  35. }
  36. catch { }
  37. }
  38. if (!string.IsNullOrEmpty(responseData))
  39. {
  40. return responseData;
  41. }
  42. throw;
  43. }
  44. }
  45. /// <summary>
  46. /// 同步方式发起http get请求
  47. /// </summary>
  48. /// <param name="url">请求URL</param>
  49. /// <param name="queryString">参数字符串</param>
  50. /// <returns>请求返回值</returns>
  51. public static string HttpGet(string url, string queryString = null)
  52. {
  53. if (!string.IsNullOrEmpty(queryString))
  54. url += "?" + queryString.Trim(' ', '?', '&');
  55. var webRequest = WebRequest.Create(url) as HttpWebRequest;
  56. if (webRequest == null) return null;
  57. webRequest.Method = "GET";
  58. webRequest.ContinueTimeout = 200000;
  59. return WebResponseGet(webRequest);
  60. }
  61. /// <summary>
  62. /// 同步方式发起http get请求
  63. /// </summary>
  64. /// <param name="url">请求URL</param>
  65. /// <param name="paras">请求参数列表</param>
  66. /// <returns>请求返回值</returns>
  67. public static string HttpGet(string url, Dictionary<string, string> paras)
  68. {
  69. var querystring = GetQueryFromParas(paras);
  70. return HttpGet(url, querystring);
  71. }
  72. /// <summary>
  73. /// 同步方式发起http post请求
  74. /// </summary>
  75. /// <param name="url">请求URL</param>
  76. /// <param name="queryString">参数字符串</param>
  77. /// <returns>请求返回值</returns>
  78. public static string HttpPost(string url, string queryString = null)
  79. {
  80. HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
  81. if (webRequest == null) return null;
  82. webRequest.Method = "POST";
  83. webRequest.ContentType = "application/x-www-form-urlencoded";
  84. webRequest.ContinueTimeout = 20000;
  85. //POST the data.
  86. using (var requestWriter = new StreamWriter(webRequest.GetRequestStreamAsync().Result))
  87. {
  88. requestWriter.Write(queryString);
  89. }
  90. return WebResponseGet(webRequest);
  91. }
  92. /// <summary>
  93. /// 同步方式发起http post请求
  94. /// </summary>
  95. /// <param name="url">请求URL</param>
  96. /// <param name="paras">请求参数列表</param>
  97. /// <returns>请求返回值</returns>
  98. public static string HttpPost(string url, Dictionary<string, string> paras)
  99. {
  100. var querystring = GetQueryFromParas(paras);
  101. return HttpPost(url, querystring);
  102. }
  103. /// <summary>
  104. /// Dictionary参数组合成字符串
  105. /// </summary>
  106. /// <param name="paras"></param>
  107. public static string GetQueryFromParas(Dictionary<string, string> paras)
  108. {
  109. if (paras == null || paras.Count == 0) return "";
  110. var buffer = new StringBuilder();
  111. var count = 1;
  112. foreach (var key in paras.Keys)
  113. {
  114. buffer.AppendFormat("{0}={1}", key, Uri.EscapeDataString(paras[key]));
  115. if (count < paras.Count)
  116. {
  117. buffer.Append("&");
  118. }
  119. count++;
  120. }
  121. return buffer.ToString(); ;
  122. }
  123. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注