赞
踩
前言
HttpWebRequest是一个常用的类,用于发送和接收HTTP请求。在C#中使用HttpWebRequest可以实现各种功能,包括发送GET和POST请求、处理Cookie、设置请求头、添加参数等。本文将深入介绍HttpWebRequest的用法,并给出一些常见的示例。
使用HttpWebRequest发送GET请求非常简单,只需指定目标URL即可。以下是发送GET请求的示例代码:
using System; using System.IO; using System.Net; class Program { static void Main() { string url = "https://www.example.com/api/data"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseData = streamReader.ReadToEnd(); Console.WriteLine(responseData); } } }
上述代码中,首先通过WebRequest.Create
方法创建一个HttpWebRequest
实例,并指定目标URL。然后设置请求方法为GET,通过GetResponse
方法发送请求并获取响应。最后通过StreamReader
对象读取响应数据。
发送POST请求与发送GET请求类似,只需将请求方法设置为POST,并设置请求体的数据。以下是发送POST请求的示例代码:
using System; using System.IO; using System.Net; using System.Text; class Program { static void Main() { string url = "https://www.example.com/api/data"; string requestData = "param1=value1¶m2=value2"; byte[] byteData = Encoding.UTF8.GetBytes(requestData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteData.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(byteData, 0, byteData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseData = streamReader.ReadToEnd(); Console.WriteLine(responseData); } } }
上述代码中,首先将请求体的数据转换为字节数组,并设置请求方法为POST。然后设置Content-Type
头为application/x-www-form-urlencoded
,并设置请求体的长度。接着通过GetRequestStream
方法获取请求流,并将请求数据写入请求流中。发送请求并获取响应的过程与发送GET请求相同。
HttpWebRequest还提供了一些方法和属性来设置请求头和处理Cookie。以下是设置请求头和处理Cookie的示例代码:
using System; using System.IO; using System.Net; class Program { static void Main() { string url = "https://www.example.com/api/data"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; // 设置请求头 request.Headers["Authorization"] = "Bearer xxxxxxx"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"; // 处理Cookie request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(new Uri(url), new Cookie("cookie1", "value1")); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseData = streamReader.ReadToEnd(); Console.WriteLine(responseData); } } }
上述代码中,使用request.Headers
属性可以设置请求头的键值对。通过设置UserAgent
属性可以伪装请求的浏览器标识。通过创建CookieContainer
对象并使用Add
方法添加Cookie来处理Cookie。
使用HttpWebRequest发送请求时,可能需要添加一些参数和在接收到响应后进行处理。以下是添加参数和处理响应的示例代码:
using System; using System.IO; using System.Net; using System.Text; class Program { static void Main() { string url = "https://www.example.com/api/data"; string requestData = "param1=value1¶m2=value2"; byte[] byteData = Encoding.UTF8.GetBytes(requestData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteData.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(byteData, 0, byteData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // 处理响应 HttpStatusCode statusCode = response.StatusCode; string statusDescription = response.StatusDescription; using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { string responseData = streamReader.ReadToEnd(); Console.WriteLine(responseData); } } }
以上代码中,发送POST请求时添加了一些参数,然后获取响应的状态码、状态描述和响应数据。
使用C#中的HttpWebRequest可以轻松发送和接收HTTP请求,实现各种功能。本文介绍了HttpWebRequest的用法,包括发送GET和POST请求、处理Cookie、设置请求头、添加参数等。可以根据需要进行灵活运用,实现更强大的HTTP请求功能。希望本文对你有所帮助!
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。