简体   繁体   English

如何在 Visual C# 中发送 HTTP 2.0 请求

[英]How to send HTTP 2.0 request in Visual C#

I am trying to send HTTP 2.0 request in Visual C#.我正在尝试在 Visual C# 中发送 HTTP 2.0 请求。

I am using latest version .NET Framework.我正在使用最新版本的 .NET Framework。 In Edge browser's Developer Tool, ' https://www.google.com ' website is showing as HTTP/2.在 Edge 浏览器的开发者工具中,“ https://www.google.com ” 网站显示为 HTTP/2。

But below code is throwing HTTP version as 1.1.但是下面的代码将 HTTP 版本抛出为 1.1。 I have added relevant User-Agent string in the request.我在请求中添加了相关的 User-Agent 字符串。 What am I missing here?我在这里缺少什么?

        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);
    }

HTTP 2.0 support in Framework 4.6.2 version only.仅在 Framework 4.6.2 版本中支持 HTTP 2.0。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM