C# 使用WebClient发送请求 作者:马育民 • 2023-10-16 19:23 • 阅读:10104 # 发get请求 ### 返回html字符串 ``` WebClient client = new WebClient(); // 指定字符集 client.Encoding = System.Text.Encoding.UTF8; // 必须携带下面数据,否则发送请求,报错:服务器500 client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60"; client.Headers["Referer"] = url; // 获取响应字符串 string html = client.DownloadString(url); // 获取响应头 //WebHeaderCollection resp = client.ResponseHeaders; //string te = resp.Get("Transfer-Encoding"); //string ct = resp.Get("Content-Type"); //string ce = resp.Get("Content-Encoding"); //string html = Encoding.UTF8.GetString(data); ``` ### 返回字节数组 返回字节数组,然后进行解码 ``` WebClient client = new WebClient(); // 必须携带下面数据,否则发送请求,报错:服务器500 client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60"; client.Headers["Referer"] = url; byte[] data = client.DownloadData(url); string html = Encoding.UTF8.GetString(data); ``` ### 携带请求头 每次发请求,都需要携带请求头。如果只携带一次请求头,那么发请求后,就会清空请求头 ``` WebClient client = new WebClient(); // 指定字符集 client.Encoding = System.Text.Encoding.UTF8; // 必须携带下面数据,否则发送请求,报错:服务器500 client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60"; client.Headers["Referer"] = url; // 获取响应字符串 string html = client.DownloadString(url); ``` # 下载文件 ``` WebClient client = new WebClient(); // 指定字符集 client.Encoding = System.Text.Encoding.UTF8; // 必须携带下面数据,否则发送请求,报错:服务器500 client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60"; client.Headers["Referer"] = url; // 下载文件,需指定下载的文件名 client.DownloadFile(downloadUrl, "a.zip"); ``` # 解压缩gzip 如果web服务器返回的数据经过 gzip 压缩,就需要 继承 `WebClient` 类,并覆盖 `GetWebRequest()` 方法,才能解压缩 ### 定义子类 ``` internal class GZipWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; return request; } } ``` ### 调用 然后使用 `GZipWebClient` 发请求 ``` GZipWebClient client = new GZipWebClient(); client.Encoding = System.Text.Encoding.UTF8;//定义语言 // 必须携带下面数据,否则发送请求,报错:服务器500 client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60"; client.Headers["Referer"] = url; //client.Headers["Accept-Encoding"] = "gzip, deflate, br"; string html = client.DownloadString(url); ``` 参考: https://blog.csdn.net/weixin_42301080/article/details/118394491 原文出处:http://www.malaoshi.top/show_1IX6QS1ybygh.html