Unity使用https请求握手失败的处理方案

发布时间 2023-10-09 18:47:40作者: 威少小二orz

Unity使用https请求握手失败的处理方案

Handshake failed UNITYTLS_INTERNALS解决方案

        var handler = new HttpClientHandler();//需要加这句
        handler.ClientCertificateOptions = ClientCertificateOption.Manual;//需要加这句
        
        using (HttpClient httpClient = new HttpClient(handler))//httpClient里带handler参数
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);//这里调用的是下面那个方法
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;//这句也要加
            var content = new MultipartFormDataContent();
            //var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
            content.Headers.Add("ContentType", "multipart/form-data");//声明头部
            content.Add(new StringContent("1"), "a");

            httpClient.DefaultRequestHeaders.Add("Method", "Post");
            

            HttpResponseMessage response = await httpClient.PostAsync("https://127.0.0.1:5555/api/WeatherForecast/GameTest", content);//Debug.Log("状态码0:" + response.StatusCode);
            response.EnsureSuccessStatusCode();
            //Debug.Log("状态码1:" + response.StatusCode);
            string responseBody = await response.Content.ReadAsStringAsync();
            Debug.Log("这里是.net core web api测试:" + responseBody);

        }
    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true; //总是接受     
    }