HttpClient Received an unexpected EOF or 0 bytes from the transport stream

Posted wu_u

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient Received an unexpected EOF or 0 bytes from the transport stream相关的知识,希望对你有一定的参考价值。

请求https链接时报错,奇怪的是pc1正常,pc2异常

Unhandled Exception: System.AggregateException: One or more errors occurred. ( Received an unexpected EOF or 0 bytes from the transport stream.) ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
at System.Net.Security.SslStreamInternal.<FillBufferAsync>g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)

 

原因:大概是请求时默认了一个过时了的ssl协议

解决:指定tls 1.0或其他有效协议,参考https://stackoverflow.com/questions/25414907/authenticateasclient-system-io-ioexception-received-an-unexpected-eof-or-0-byt

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        var html = GetClientStringAsync("https://**********").Result;
        Console.Write(html);
        Console.ReadLine();
    }

    public static async Task<string> GetClientStringAsync(string url, string encoding = "utf-8")
    {
        var result = string.Empty;
        var httpClientHandler = new HttpClientHandler
        {
            SslProtocols = System.Security.Authentication.SslProtocols.Tls
        };
        HttpClient client = new HttpClient(httpClientHandler);
        client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
        var bytes = await client.GetByteArrayAsync(url);
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        result = Encoding.GetEncoding(encoding).GetString(bytes);
        return result;
    }
}

 

以上是关于HttpClient Received an unexpected EOF or 0 bytes from the transport stream的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch报错: received plaintext http traffic on an https channel, closing connection ...

手机上网出现 Gateway received an linvalid response from the upstream gateway 是啥意思 怎么解决?

解决 “message“:“An invalid response was received from the upstream server“

解决 “message“:“An invalid response was received from the upstream server“

手机上百度显示Gateway received an invalid response from the upstream gateway怎么回事???

UICollectionView received layout attributes for a cell with an index path that does not exist