当前位置:   article > 正文

C#的WebRequest类_c# request.contenttype

c# request.contenttype

C#的WebRequest类是一个抽象基类,用于发送 Web 请求并与远程服务器进行交互。它提供了许多方法和属性来配置和执行 HTTP 请求。

下面是使用WebRequest类发送简单的 HTTP GET 请求的示例:

  1. string url = "https://api.example.com/data";
  2. WebRequest request = WebRequest.Create(url);
  3. request.Method = "GET";
  4. using (WebResponse response = request.GetResponse())
  5. {
  6. using (Stream dataStream = response.GetResponseStream())
  7. {
  8. using (StreamReader reader = new StreamReader(dataStream))
  9. {
  10. string responseContent = reader.ReadToEnd();
  11. Console.WriteLine(responseContent);
  12. }
  13. }
  14. }

这个示例中,我们首先创建一个WebRequest对象,并设置请求的 URL 和请求方法为 GET。然后,使用GetResponse()方法获取服务器的响应。

通过获取响应对象的GetResponseStream()方法,我们可以获得响应的数据流。然后,我们使用StreamReader读取数据流,并将响应内容作为字符串打印出来。

需要注意的是,在使用完WebResponseStreamStreamReader后,我们使用using语句来确保资源的正确释放。

除了发送简单的 GET 请求,WebRequest类还提供了其他方法和属性,可以用于发送 POST 请求、设置请求头、处理响应等操作。

MESHelper基于WebRequest实现发送Web请求并得到Respond数据

  1. public static string HttpRequest(string url, string body, int timeout = 60000,
  2. HttpEncoding encoding = HttpEncoding.UTF8,
  3. HttpMethod method = HttpMethod.POST,
  4. HttpContentType contenttype = HttpContentType.Form)
  5. {
  6. // 检查服务器地址和发送数据是否为空
  7. if (String.IsNullOrWhiteSpace(url))
  8. {
  9. throw new ArgumentNullException("server url can't be empty...");
  10. }
  11. if (String.IsNullOrWhiteSpace(body))
  12. {
  13. throw new ArgumentNullException("send body can't be empty...");
  14. }
  15. HttpWebRequest request = null;
  16. try
  17. {
  18. // 根据请求方法创建 HttpWebRequest 对象
  19. if (method == HttpMethod.POST)
  20. request = (HttpWebRequest)WebRequest.Create(url);
  21. else
  22. request = (HttpWebRequest)WebRequest.Create(body);
  23. // 设置超时时间
  24. if (timeout != 0)
  25. {
  26. request.Timeout = timeout;
  27. request.ReadWriteTimeout = timeout;
  28. }
  29. request.Method = method.ToString();
  30. // 设置内容类型
  31. switch (contenttype)
  32. {
  33. case HttpContentType.Form:
  34. request.ContentType = "application/x-www-form-urlencoded";
  35. break;
  36. case HttpContentType.Json:
  37. request.ContentType = "application/json";
  38. break;
  39. case HttpContentType.Xml:
  40. request.ContentType = "application/xml";
  41. break;
  42. case HttpContentType.Text:
  43. request.ContentType = "text/plain";
  44. break;
  45. default:
  46. request.ContentType = "application/x-www-form-urlencoded";
  47. break;
  48. }
  49. // 添加请求到缓存列表
  50. lock (gHttpCache)
  51. {
  52. gHttpCache.Add(request);
  53. }
  54. var encod = GetEncoding(encoding);
  55. if (method == HttpMethod.POST)
  56. {
  57. // 将数据转换为字节数组并设置请求内容长度
  58. byte[] buffer = encod.GetBytes(body);
  59. request.ContentLength = buffer.Length;
  60. using (var stream = request.GetRequestStream())
  61. {
  62. // 发送请求数据
  63. stream.Write(buffer, 0, buffer.Length);
  64. }
  65. }
  66. // 发送请求并获取响应
  67. var response = (HttpWebResponse)request.GetResponse();
  68. System.IO.Stream s = response.GetResponseStream();
  69. if (s != null)
  70. {
  71. var reader = new System.IO.StreamReader(s, encod);
  72. // 读取响应数据
  73. string responseString = reader.ReadToEnd();
  74. s.Close();
  75. reader.Close();
  76. return responseString;
  77. }
  78. return String.Empty;
  79. }
  80. catch (Exception ex)
  81. {
  82. // 捕获异常并返回异常信息
  83. return ex.ToString();
  84. }
  85. finally
  86. {
  87. if (request != null)
  88. {
  89. // 从缓存列表中移除请求
  90. lock (gHttpCache)
  91. {
  92. gHttpCache.Remove(request);
  93. }
  94. }
  95. }
  96. }

这是一个用于发送 HTTP 请求的方法。它接受以下参数:

  • url:请求的 URL 地址。
  • body:请求体,即需要发送的数据。
  • timeout:超时时间,默认为 60000 毫秒。
  • encoding:请求和响应的编码方式,默认为 UTF-8。
  • method:请求方法,默认为 POST。
  • contenttype:请求的内容类型,默认为表单形式。

方法首先检查 URL 和请求体是否为空,然后根据提供的参数创建一个HttpWebRequest对象。如果指定了超时时间,设置请求的超时时间和读写超时时间。

根据请求方法和内容类型,设置请求的方法和内容类型头。默认情况下,内容类型为application/x-www-form-urlencoded

接下来,根据指定的编码方式获取编码对象,并根据请求方法进行相应的处理。对于 POST 请求,将请求体转换为字节数组,并设置请求的内容长度和请求流。然后,获取服务器的响应,并将响应内容读取为字符串。

最后,返回响应字符串。如果发生异常,捕获异常并返回异常信息。

在方法的最后,通过finally块确保正确地从缓存中移除请求对象。

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

闽ICP备14008679号