microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.36k stars 2.22k forks source link

[SOS] Intermittent Connection Issues with HttpClient in .NET 6 #1434

Closed yuefengkai closed 5 months ago

yuefengkai commented 5 months ago

Hello,

I am experiencing intermittent connectivity issues when making requests to https://api.maersk.com/ using HttpClient in .NET 6. While I can occasionally establish a connection, most attempts fail. Interestingly, when I make the same requests using Python code, the connection is consistently successful.

Here are some troubleshooting steps I have already tried:

Modifying the HTTP protocol version. Changing HTTP headers. Confirmed that I can successfully ping and curl the URL from my location in Hong Kong. Despite these efforts, the issue persists specifically with C# HttpClient. Below is the code snippet I am using:

Could you help me identify what might be causing this inconsistency?

Thank you for your assistance. image image

 using System.Net;
using System.Diagnostics;
using System.Net.Http;

Console.WriteLine("Hello, World!");

var httpclietHandel = new HttpClientHandler
{

        AutomaticDecompression = System.Net.DecompressionMethods.GZip,
        ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; },
         SslProtocols = System.Security.Authentication.SslProtocols.Tls13
};
var client = new HttpClient(httpclietHandel)
{
        //DefaultRequestVersion = HttpVersion.Version11,
        //  DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower
};
var request = new HttpRequestMessage
{
        Method = HttpMethod.Get,
        RequestUri = new Uri("https://api.maersk.com/"),
        Headers =
                                {
                                                                { "Consumer-Key", "xxxxxxxxxx" },
                                                                {"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"},
                                                                {"Accept-Encoding","gzip, deflate, br, zstd"},
                                                                {"User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"},
                                                                {"Accept-Language","zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7"},
                },
};

Stopwatch sw = Stopwatch.StartNew();

using (var response = await client.SendAsync(request))
{
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();

        sw.Stop();
        Console.WriteLine($"{sw.ElapsedMilliseconds}-=--{body}");
}

Console.WriteLine("Done");
import requests
import time

payload = "{}"

headers = {
    'Consumer-Key': "xxxxxx",
    'content-type': "application/json"
    }

url = "https://api.maersk.com/"

start = time.time()
response = requests.get(url,timeout=3,headers=headers,data=payload,)

print(response.status_code)
print(response.content)
print("Finished...{}".format(time.time()-start))
vincent-waung commented 5 months ago

I have encountered the same problem and haven't found a solution yet.

danmoseley commented 5 months ago

Hello @yuefengkai as this is in .NET 6 the right repo for this is https://github.com/dotnet/runtime .. could you please close here and open there? (I can't transfer between microsoft/ and dotnet/ repos)