如何在 Visual C# 中发送 HTTP 2.0 请求
How to send HTTP 2.0 request in Visual C#
我正在尝试在 Visual C# 中发送 HTTP 2.0 请求。
我正在使用最新版本的 .NET Framework。在 Edge 浏览器的开发者工具中,“https://www.google.com”网站显示为 HTTP/2。
但下面的代码将 HTTP 版本抛出为 1.1。我在请求中添加了相关的用户代理字符串。我在这里错过了什么?
string html = string.Empty;
string url = @TextBox1.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
for (int i = 1; i < response.Headers.Count; ++i)
{
System.Diagnostics.Debug.WriteLine(response.Headers[i]);
String resp = response.Headers[i].ToString();
resp = response.Headers[i-1].ToString() + resp;
System.Diagnostics.Debug.WriteLine(response.ProtocolVersion);
TextBox2.Text = response.ProtocolVersion.ToString();
}
}
System.Diagnostics.Debug.WriteLine(html);
Console.WriteLine(html);
}
仅在 Framework 4.6.2 版本中支持 HTTP 2.0。
https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx
我正在尝试在 Visual C# 中发送 HTTP 2.0 请求。
我正在使用最新版本的 .NET Framework。在 Edge 浏览器的开发者工具中,“https://www.google.com”网站显示为 HTTP/2。
但下面的代码将 HTTP 版本抛出为 1.1。我在请求中添加了相关的用户代理字符串。我在这里错过了什么?
string html = string.Empty;
string url = @TextBox1.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;
request.Accept = "text/html, application/xhtml+xml, image/jxr, */*";
request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
for (int i = 1; i < response.Headers.Count; ++i)
{
System.Diagnostics.Debug.WriteLine(response.Headers[i]);
String resp = response.Headers[i].ToString();
resp = response.Headers[i-1].ToString() + resp;
System.Diagnostics.Debug.WriteLine(response.ProtocolVersion);
TextBox2.Text = response.ProtocolVersion.ToString();
}
}
System.Diagnostics.Debug.WriteLine(html);
Console.WriteLine(html);
}
仅在 Framework 4.6.2 版本中支持 HTTP 2.0。
https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx