赞
踩
WebRequest
类是一个抽象基类,用于发送 Web 请求并与远程服务器进行交互。它提供了许多方法和属性来配置和执行 HTTP 请求。下面是使用WebRequest
类发送简单的 HTTP GET 请求的示例:
string url = "https://api.example.com/data"; WebRequest request = WebRequest.Create(url); request.Method = "GET"; using (WebResponse response = request.GetResponse()) { using (Stream dataStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(dataStream)) { string responseContent = reader.ReadToEnd(); Console.WriteLine(responseContent); } } }这个示例中,我们首先创建一个
WebRequest
对象,并设置请求的 URL 和请求方法为 GET。然后,使用GetResponse()
方法获取服务器的响应。通过获取响应对象的
GetResponseStream()
方法,我们可以获得响应的数据流。然后,我们使用StreamReader
读取数据流,并将响应内容作为字符串打印出来。需要注意的是,在使用完
WebResponse
、Stream
和StreamReader
后,我们使用using
语句来确保资源的正确释放。除了发送简单的 GET 请求,
WebRequest
类还提供了其他方法和属性,可以用于发送 POST 请求、设置请求头、处理响应等操作。
public static string HttpRequest(string url, string body, int timeout = 60000, HttpEncoding encoding = HttpEncoding.UTF8, HttpMethod method = HttpMethod.POST, HttpContentType contenttype = HttpContentType.Form) { // 检查服务器地址和发送数据是否为空 if (String.IsNullOrWhiteSpace(url)) { throw new ArgumentNullException("server url can't be empty..."); } if (String.IsNullOrWhiteSpace(body)) { throw new ArgumentNullException("send body can't be empty..."); } HttpWebRequest request = null; try { // 根据请求方法创建 HttpWebRequest 对象 if (method == HttpMethod.POST) request = (HttpWebRequest)WebRequest.Create(url); else request = (HttpWebRequest)WebRequest.Create(body); // 设置超时时间 if (timeout != 0) { request.Timeout = timeout; request.ReadWriteTimeout = timeout; } request.Method = method.ToString(); // 设置内容类型 switch (contenttype) { case HttpContentType.Form: request.ContentType = "application/x-www-form-urlencoded"; break; case HttpContentType.Json: request.ContentType = "application/json"; break; case HttpContentType.Xml: request.ContentType = "application/xml"; break; case HttpContentType.Text: request.ContentType = "text/plain"; break; default: request.ContentType = "application/x-www-form-urlencoded"; break; } // 添加请求到缓存列表 lock (gHttpCache) { gHttpCache.Add(request); } var encod = GetEncoding(encoding); if (method == HttpMethod.POST) { // 将数据转换为字节数组并设置请求内容长度 byte[] buffer = encod.GetBytes(body); request.ContentLength = buffer.Length; using (var stream = request.GetRequestStream()) { // 发送请求数据 stream.Write(buffer, 0, buffer.Length); } } // 发送请求并获取响应 var response = (HttpWebResponse)request.GetResponse(); System.IO.Stream s = response.GetResponseStream(); if (s != null) { var reader = new System.IO.StreamReader(s, encod); // 读取响应数据 string responseString = reader.ReadToEnd(); s.Close(); reader.Close(); return responseString; } return String.Empty; } catch (Exception ex) { // 捕获异常并返回异常信息 return ex.ToString(); } finally { if (request != null) { // 从缓存列表中移除请求 lock (gHttpCache) { gHttpCache.Remove(request); } } } }这是一个用于发送 HTTP 请求的方法。它接受以下参数:
url
:请求的 URL 地址。body
:请求体,即需要发送的数据。timeout
:超时时间,默认为 60000 毫秒。encoding
:请求和响应的编码方式,默认为 UTF-8。method
:请求方法,默认为 POST。contenttype
:请求的内容类型,默认为表单形式。方法首先检查 URL 和请求体是否为空,然后根据提供的参数创建一个
HttpWebRequest
对象。如果指定了超时时间,设置请求的超时时间和读写超时时间。根据请求方法和内容类型,设置请求的方法和内容类型头。默认情况下,内容类型为
application/x-www-form-urlencoded
。接下来,根据指定的编码方式获取编码对象,并根据请求方法进行相应的处理。对于 POST 请求,将请求体转换为字节数组,并设置请求的内容长度和请求流。然后,获取服务器的响应,并将响应内容读取为字符串。
最后,返回响应字符串。如果发生异常,捕获异常并返回异常信息。
在方法的最后,通过
finally
块确保正确地从缓存中移除请求对象。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。