当前位置:   article > 正文

C#-HTTP请求GET_c# http get

c# http get
  1. public static string SendRequestUTF8(string url, string para, string method, int timeout, string ContentType, ref string Status, string Tag, string LogName)
  2. {
  3. Status = "OK";
  4. string strResult = "";
  5. if (url == null || url == "")
  6. return null;
  7. if (method == null || method == "")
  8. method = "GET";
  9. // GET方式
  10. if (method.ToUpper() == "GET")
  11. {
  12. try
  13. {
  14. System.GC.Collect();
  15. //设置最大连接数
  16. ServicePointManager.DefaultConnectionLimit = 200;
  17. //设置https验证方式
  18. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  19. {
  20. ServicePointManager.ServerCertificateValidationCallback =
  21. new RemoteCertificateValidationCallback(CheckValidationResult);
  22. }
  23. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
  24. System.Net.WebRequest wrq = System.Net.WebRequest.Create(url + para);
  25. wrq.Method = "GET";
  26. if (timeout != 0)
  27. {
  28. wrq.Timeout = timeout;
  29. }
  30. using (System.Net.WebResponse wrp = wrq.GetResponse())
  31. {
  32. using (System.IO.StreamReader sr = new System.IO.StreamReader(wrp.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
  33. {
  34. strResult = sr.ReadToEnd();
  35. sr.Close();
  36. sr.Dispose();
  37. }
  38. wrp.Close();
  39. }
  40. wrq.Abort();
  41. }
  42. catch (WebException ex)
  43. {
  44. if (LogName != null)
  45. {
  46. T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
  47. , LogName);
  48. }
  49. Status = ex.Status.ToString();
  50. return ex.Message;
  51. }
  52. catch (Exception ex)
  53. {
  54. if (LogName != null)
  55. {
  56. T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
  57. , LogName);
  58. }
  59. Status = "T9_9999";
  60. return ex.Message;
  61. }
  62. }
  63. // POST方式
  64. else if (method.ToUpper() == "POST")
  65. {
  66. try
  67. {
  68. if (para.Length > 0 && para.IndexOf('?') == 0)
  69. {
  70. para = para.Substring(1);
  71. }
  72. WebRequest req = WebRequest.Create(url);
  73. req.Method = "POST";
  74. if (timeout != 0)
  75. {
  76. req.Timeout = timeout;
  77. }//application/x-www-form-urlencoded
  78. req.ContentType = string.IsNullOrWhiteSpace(ContentType) ? "application/x-www-form-urlencoded" : ContentType;
  79. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  80. {
  81. ServicePointManager.ServerCertificateValidationCallback =
  82. new RemoteCertificateValidationCallback(CheckValidationResult);
  83. }
  84. StringBuilder UrlEncoded = new StringBuilder();
  85. Char[] reserved = { '?', '=', '&' };
  86. byte[] SomeBytes = null;
  87. if (para != null)
  88. {
  89. int i = 0, j;
  90. while (i < para.Length)
  91. {
  92. j = para.IndexOfAny(reserved, i);
  93. if (j == -1)
  94. {
  95. UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.GetEncoding("utf-8")));
  96. break;
  97. }
  98. UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.GetEncoding("utf-8")));
  99. UrlEncoded.Append(para.Substring(j, 1));
  100. i = j + 1;
  101. }
  102. SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
  103. req.ContentLength = SomeBytes.Length;
  104. Stream newStream = req.GetRequestStream();
  105. newStream.Write(SomeBytes, 0, SomeBytes.Length);
  106. newStream.Close();
  107. }
  108. else
  109. {
  110. req.ContentLength = 0;
  111. }
  112. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
  113. WebResponse result = req.GetResponse();
  114. Stream ReceiveStream = result.GetResponseStream();
  115. Byte[] read = new Byte[512];
  116. int bytes = ReceiveStream.Read(read, 0, 512);
  117. while (bytes > 0)
  118. {
  119. // 注意:
  120. // 下面假定响应使用 UTF-8 作为编码方式。
  121. // 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
  122. // Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
  123. Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
  124. strResult += encode.GetString(read, 0, bytes);
  125. bytes = ReceiveStream.Read(read, 0, 512);
  126. }
  127. return strResult;
  128. }
  129. catch (WebException ex)
  130. {
  131. if (LogName != null)
  132. {
  133. T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
  134. , LogName);
  135. }
  136. Status = ex.Status.ToString();
  137. if (ex.Response != null)
  138. {
  139. Stream ReceiveStream = ex.Response.GetResponseStream();
  140. Byte[] read = new Byte[512];
  141. int bytes = ReceiveStream.Read(read, 0, 512);
  142. while (bytes > 0)
  143. {
  144. // 注意:
  145. // 下面假定响应使用 UTF-8 作为编码方式。
  146. // 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
  147. // Encoding encode = System.Text.Encoding.GetEncoding("shift-jis");
  148. Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
  149. strResult += encode.GetString(read, 0, bytes);
  150. bytes = ReceiveStream.Read(read, 0, 512);
  151. }
  152. }
  153. return strResult;
  154. }
  155. catch (Exception ex)
  156. {
  157. if (LogName != null)
  158. {
  159. T9.Util.LogUtil.WriteLog("SendRequestUTF8-发生未知错误" + (Tag != null ? "[" + Tag + "]" : "") + ":\r\n" + ex.Message.ToString() + "\r\n报错详情:" + ex.StackTrace.ToString()
  160. , LogName);
  161. }
  162. Status = "T9_9999";
  163. return ex.Message;
  164. }
  165. }
  166. return strResult;
  167. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/259664
推荐阅读
相关标签
  

闽ICP备14008679号