Skip to content

The server returned an invalid or unrecognized response" error when using System.Net.Http.HttpClient #19825

@Petermarcu

Description

@Petermarcu

Getting this bug into the right place: dotnet/core#321

@corinas
Hello,

I am facing a problem when making concurrent requests to a web api when using dotnet core. Sometimes i'm getting the below error; the error is not happening all the time (usually it happens once or twice in 600 concurrent calls).

System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

Here's the test method I use for testing:

    [Fact]
    public void PostToStardogParallelTaskWithSendAsync()
    {
        int x = 600;
        Task[] allTasks = new Task[x];

        using (var httpClient = new HttpClient())
        {
            var byteArray = Encoding.ASCII.GetBytes($"user:password");                

            for (int k = 0; k < 1; k++)
            {
                for (int i = 0; i < x; i++)
                {
                    System.Diagnostics.Debug.WriteLine($"Starting tsk {i}...");
                    allTasks[i] = Task.Factory.StartNew(async (ii) =>
                    {
                        var d = DateTime.Now;
                        var query = $"INSERT DATA {{ <urn:subj> <urn:pred> <urn:obj>. }}";
                        var content = new StringContent(query, Encoding.UTF8, "application/sparql-update");

                        HttpRequestMessage rm = new HttpRequestMessage(HttpMethod.Post, "MyStardogWebApiUrl");
                        rm.Headers.TryAddWithoutValidation("accept", "text/boolean");
                        rm.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                        rm.Content = content;                            
                        try
                        {
                            var response = await httpClient.SendAsync(rm);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine($"\nThe following exception have been thrown: {e}");                            
                        }

                        System.Diagnostics.Debug.WriteLine($"Task {ii} ended in: {(DateTime.Now - d).TotalMilliseconds}.");

                    }, i).Unwrap();
                }
                try
                {
                    Task.WaitAll(allTasks);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"nThe following exception have been thrown: {e}");                       
                }
            }
        }
    }

When this error occurs, the response looks like this:
{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: , Headers:{}}

Calling the same method from a testing project that uses dotnet framework 4.5 does not cause any issue.

Could you please advise?
Thanks!

Corina

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions