C#如何正确发出http Web GET请求

C#如何正确发出http Web GET请求,第1张

C#如何正确发出http Web GET请求

服务器有时会压缩其响应以节省带宽,当这种情况发生时,您需要在尝试读取响应之前对其进行解压缩。幸运的是,.NET框架可以自动执行此 *** 作,但是,我们必须打开该设置。

这是如何实现此目的的示例

string html = string.Empty;string url = @"https://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflow";HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.AutomaticDecompression = DecompressionMethods.GZip;using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())using (Stream stream = response.GetResponseStream())using (StreamReader reader = new StreamReader(stream)){    html = reader.ReadToEnd();}Console.WriteLine(html);

编辑 17/11/23

更新以抛出使用GET请求和POST异步的示例


得到

public string Get(string uri){    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())    using(Stream stream = response.GetResponseStream())    using(StreamReader reader = new StreamReader(stream))    {        return reader.ReadToEnd();    }}

取得异步

public async Task<string> GetAsync(string uri){    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;    using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())    using(Stream stream = response.GetResponseStream())    using(StreamReader reader = new StreamReader(stream))    {        return await reader.ReadToEndAsync();    }}

POST 在您希望使用其他HTTP方法(例如PUT,DELETE和ETC)时
包含参数

method

public string Post(string uri, string data, string contentType, string method = "POST"){    byte[] dataBytes = Encoding.UTF8.GetBytes(data);    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;    request.ContentLength = dataBytes.Length;    request.ContentType = contentType;    request.Method = method;    using(Stream requestBody = request.GetRequestStream())    {        requestBody.Write(dataBytes, 0, dataBytes.Length);    }    using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())    using(Stream stream = response.GetResponseStream())    using(StreamReader reader = new StreamReader(stream))    {        return reader.ReadToEnd();    }}

POST异步 在您希望使用其他HTTP方法(例如PUT,DELETE和ETC)时
包含参数

method

public async Task<string> PostAsync(string uri, string data, string contentType, string method = "POST"){    byte[] dataBytes = Encoding.UTF8.GetBytes(data);    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;    request.ContentLength = dataBytes.Length;    request.ContentType = contentType;    request.Method = method;    using(Stream requestBody = request.GetRequestStream())    {        await requestBody.WriteAsync(dataBytes, 0, dataBytes.Length);    }    using(HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())    using(Stream stream = response.GetResponseStream())    using(StreamReader reader = new StreamReader(stream))    {        return await reader.ReadToEndAsync();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5150447.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存