.NET5 自定义HttpClient中https协议请求的SNI的值

static async Task Main(string[] args)
        {

            const string DNS_AND_SNI = "www.pixivision.net";

            //注意这个地方是http而不是httpps
            //否则我们不能自定义sslstream
            const string URI = "http://www.pixiv.net/";

            var handler = new SocketsHttpHandler()
            {

                ConnectCallback = (info, token) =>
                {

                    Console.WriteLine(info.InitialRequestMessage.RequestUri);

                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    socket.Connect(DNS_AND_SNI, 443);

                    var stream = new NetworkStream(socket, true);

                    SslStream sslstream = new SslStream(stream, false);

                    sslstream.AuthenticateAsClient(new SslClientAuthenticationOptions
                    {
                        //sni字符串
                        TargetHost = DNS_AND_SNI,

                        //tls指定后续的应用层协议
                        ApplicationProtocols = new List<SslApplicationProtocol>(new SslApplicationProtocol[] { SslApplicationProtocol.Http2 })
                    });


                    return new ValueTask<Stream>(sslstream);
                }
            };


            HttpClient client = new HttpClient(handler);

            //协议和tls预先指定的保持一致
            client.DefaultRequestVersion = HttpVersion.Version20;


            string s = await client.GetStringAsync(URI);

            Console.WriteLine(s.Length);
        }

 

.NET5 自定义HttpClient中https协议请求的SNI的值

上一篇:会畅通讯登陆创业板上市,CEO黄元庚说云视频是下一个万亿级市场


下一篇:.net项目开发经验