简体   繁体   中英

C# Post requests got broken

I have a program, which makes a Post request. This project worked perfectly, but with old versions of .NETCORE (1.1) , I've upgraded everything including the framework to 2.1 . Didn't change the code what so ever, no error occurs and it seemed as the change is ok .

Now my Post request is failing (I'll attach only one, but I have about 4 Post requests, everyone of them fails for the same reason)

EDIT: I've just tested my Get requests as well, the produce the same error ..

This is my code :

using (var client = new HttpClient())
{
    Field fld = new Field()
    {
        ...
   };
    ForJSON bodyFormat = new ForJSON()
    {
        ...
    };
    string output = JsonConvert.SerializeObject(bodyFormat);
    StringContent sc = new StringContent(output, Encoding.UTF8, "application/json");
    try
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                            Convert.ToBase64String(
                                                                                            Encoding.ASCII.GetBytes(
                                                                                                                                       string.Format("{0}:{1}", "XX", "YY"))));
        response = await client.PostAsync("https:...", sc); // It fails here

The error message is :

attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I've tried googling this error , but every post doesn't seems relavent to me.. They're talking about permissions firewalls and ports, but why would it work in one project and in the other wouldn't ?

If I try via Postman / my backup of the the project with 1.1 version , then it works .

Why is this happening ? What got broken? Is there maybe a timeout attribute I need to add?

attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

For this error, it is caused by the changes between core 1.1 and core 2.1 that HttpClientHandler changes from WinHttpHandler to SocketsHttpHandler .

For a workaround, you may try to configure a process to use the older HttpClientHandler

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
using (var client = new HttpClient())

Reference: Networking Performance .

Bug tracking: .NET Core 2.1 SocketsHttpHandler proxy authentication using Windows auth is broken #30166

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM