如何在Visual C#中发送HTTP 2.0请求 [英] How to send HTTP 2.0 request in Visual C#

查看:1348
本文介绍了如何在Visual C#中发送HTTP 2.0请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual C#中发送HTTP 2.0请求.

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

我正在使用最新版本的.NET Framework.在Edge浏览器的开发人员工具中," https://www.google.com 网站显示为HTTP/2 .

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

但是下面的代码抛出了HTTP版本1.1.我在请求中添加了相关的User-Agent字符串.我在这里想念什么?

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

推荐答案

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

HTTP 2.0 support in Framework 4.6.2 version only.

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

这篇关于如何在Visual C#中发送HTTP 2.0请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆