【问题标题】:How to send HTTP 2.0 request in Visual C#如何在 Visual C# 中发送 HTTP 2.0 请求
【发布时间】:2017-08-03 02:32:41
【问题描述】:

我正在尝试在 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);
    }

【问题讨论】:

标签: c# asp.net https user-agent http2


【解决方案1】:

仅在 Framework 4.6.2 版本中支持 HTTP 2.0。

https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx

【讨论】:

  • 但我只使用 4.6.2 版本。
猜你喜欢
  • 2020-01-29
  • 2018-03-05
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多