简体   繁体   中英

How to send HTTP 2.0 request in Visual C#

I am trying to send HTTP 2.0 request in Visual C#.

I am using latest version .NET Framework. In Edge browser's Developer Tool, ' https://www.google.com ' website is showing as HTTP/2.

But below code is throwing HTTP version as 1.1. I have added relevant User-Agent string in the request. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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