C# HttpClient HTTP/2发送请求

xingyun86 2021-10-14 1719

C# HttpClient HTTP/2发送请求

            try
            {
                var recaptchaUrl = "https://www.recaptcha.net";
                var recaptchaRes = "/recaptcha/api2/reload?k=6LdeO54UAAAAAPd6Q3wIb1TMjp7_G1XKiRh7tpXo";
                var socketsHttpHandler = new SocketsHttpHandler()
                {
                    //默认是None,即不压缩
                    AutomaticDecompression = DecompressionMethods.GZip,
                };
                var httpClient = new HttpClient(socketsHttpHandler)
                {
                    // Defaults to HTTP/2
                    DefaultRequestVersion = new Version(2, 0),
                    BaseAddress = new Uri(recaptchaUrl),
                };
                httpClient.DefaultRequestHeaders.Add("Host", "www.recaptcha.net");
                httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
                httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
                httpClient.DefaultRequestHeaders.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
                httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0");
                //httpClient.DefaultRequestHeaders.Add("Origin", "https://www.klm.com.cn");
                httpClient.DefaultRequestHeaders.Add("Connection","keep-alive");
                httpClient.DefaultRequestHeaders.Add("Sec-Fetch-Dest","empty");
                httpClient.DefaultRequestHeaders.Add("Sec-Fetch-Mode","no-cors");
                httpClient.DefaultRequestHeaders.Add("Sec-Fetch-Site","cross-site");
                httpClient.DefaultRequestHeaders.Add("Pragma","no-cache");
                httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                using (var httpContent = new StringContent(await System.IO.File.ReadAllTextAsync(@"postData.data", Encoding.Default), Encoding.Default, "application/x-protobuffer"))
                {
                    using (var response = await httpClient.PostAsync(recaptchaRes, httpContent))
                    {
                        response.EnsureSuccessStatusCode();//用来抛异常的
                        Console.WriteLine(response.Content);
                        var data = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(data);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.GetType();
            }


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回