Runtime: System.Net.Http.CurlException HttpClient PostAsync

Created on 12 Jun 2016  ·  26Comments  ·  Source: dotnet/runtime

Environment

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"

Curl version

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

Code

``` C#
var client = new HttpClient() {
BaseAddress = new Uri("http://example.com/")
};

var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair("key", "val")
});

var response = await client.PostAsync("/some.php", formContent, CancellationToken.None);
```

Target

dotnet 1.0.0-preview1-002702
"System.Net.Http": "4.0.1-rc2-24027"

Exception

Unhandled Exception: System.AggregateException: One or more errors occurred. (An error occurred while sending the request.) ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: _Failure when receiving data from the peer_
at System.Net.Http.CurlHandler.ThrowIfCURLEError(CURLcode error)
at System.Net.Http.CurlHandler.MultiAgent.FinishRequest(EasyRequest completedOperation, CURLcode messageResult)

Run Curl directly is working

curl -c cookies.txt -b cookies.txt -X POST --form "key=val" http://example.com/some.php -L

Can't get it working with dotnet, everything else just fine, except when I post FormUrlEncodedContent.
I tried different setup with HttpClientHandler, same exception.

Update!

Running with 1.0.0-preview2-003119 works fine.

area-System.Net os-linux

Most helpful comment

My workaround:

  • Do not listen on http://+:port but on http://specific-ip:port.
  • Connect to http://specific-ip:port from the client app.

All 26 comments

@ericeil can you take a look please?

@joshfree, you marked this as release notes. What's the note? Have we confirmed there's an issue? I tried the example and couldn't repro.

@stephentoub, @ericeil is supposed to take a look and reproduce the issue.

@ericeil is supposed to take a look and reproduce the issue.

I understand, which is why I'm questioning the "release notes" tag.

@stephentoub it's a war triage thing. This is a potential issue that won't block 1.0 from shipping but should be investigated and assuming it's valid should be added to the known issues list then moved to 1.1.

The other ~9 release notes bugs were investigated, doc'd, and closed yesterday; which may be why this one is standing out now. It should be resolved today as well.

Well, FWIW, I've not been able to repro this locally. I also put up the repro as a test in https://github.com/dotnet/corefx/issues/9414; it passed the test in all legs, and separately I submitted a private job on Ubuntu 16.04 to Jenkins, and it also passed the test. The error message "Failure when receiving data from the peer" is CURLE_RECV_ERROR, which typically means curl simply had trouble reading data from the network, and implies some kind of networking issue unrelated to the code consuming the libcurl library.

I just ran more PostAsync test, realized the remote got cloudflare on it. Running on Windows I get response but Ubuntu.. and because calling the curl from terminal is working (on Ubuntu), don't know how with .net doesn't. I'm currently digging more, maybe I'm missing something.

Moving to 1.1 based on current information

I sent a cloned request (headers) to bypass cloudflare, but not working (still no problem on windows).
Noone ever ran into this issue?
I still can't figure it out if it's my-miss-coding / client-side / server-side failure / curl.

@erikpowa, in your repo you're connecting to example.com. I assume you're actually connecting to a different site? Is that something you can share with us?

@stephentoub Yes, different.
It would be better in private :)
[email protected]

Thanks, @erikpowa. @ericeil and I will get in touch with you offline.

I was able to reproduce this on 1.0.0-preview1-002702. On more recent bits, this scenario seems to work. So it appears that this has already been fixed.

Any progress on this bug? I encountered this in 1.1 (I thought this had been fixed by now). I can confirm that curl works but the HttpClient throws a CurlException and cannot find the host.

@BlasVilla, the issue should have been addressed for 1.1. If you're still having trouble, could you please open a new issue with the details of the failure you're encountering, what OS you're on, a repro, etc.? Thanks!

我在centos6.8下也遇到这个问题,升级libcurl就行了。
centos6.8下自带的curl版本是7.19 ,版本过低
rpm -ivh http://mirror.city-fan.org/ftp/contrib/yum-repo/city-fan.org-release-1-13.rhel6.noarch.rpm
yum update libcurl
升级后的版本是libcurl-7.52

@BlasVilla Did you posted a new issue for this? We also encountered this bug in a console app with .Net Core 1.1 (netcoreapp1.1) in a standalone application running on Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-70-generic x86_64) the problem seems to happen randomly.

We get this error randomly when downloading a lot of files from Amazon S3 using AWSSDK.Core (3.3.8.2) and AWSSDK.S3 (3.3.5.7).

System.Net.Http.CurlException: Failure when receiving data from the peer
at System.Net.Http.CurlHandler.ThrowIfCURLEError(CURLcode error)
at System.Net.Http.CurlHandler.MultiAgent.FinishRequest(StrongToWeakReference`1 easyWrapper, CURLcode messageResult)

Here is the version of curl installed on our system:

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

Here is the code that seems to produce the error:
```
// Prepare S3 request
GetObjectRequest request = new GetObjectRequest();
request.Key = remoteFile;
request.BucketName = bucketName;

            // Download our file from S3
            Task<GetObjectResponse> response = client.GetObjectAsync(request);

            if (response.Result.ResponseStream != null)
            {
                using (MemoryStream memstream = new MemoryStream())
                {
                    // Get the stream
                    response.Result.ResponseStream.CopyTo(memstream);
                    byte[] bytes = memstream.ToArray();`

EDIT: Scratch that, I'm going to do a bit more investigation as it might be another issue.

Also i have this problem in Linux(Mint) with requests from a Console Application to a Local Web Server.

I have also received: System.Net.Http.CurlException: Couldn't resolve host name
running my dockerized asp.netcore mvc application on a ubuntu 16.04 LTS host.

Where:

  • the host COULD ping the dns name
  • the container COULD ping the dns name
  • the container COULD curl the request

The dns name was internal and I had specified 2 nameservers only one of which would resolve the internal dns name. After removing the secondary dns nameserver from my network interface on the HOST machine: /etc/network/interfaces (restart required). I confirmed the fix by re-enabling the secondary nameserver again and the issue returned.

Hope that helps someone in the future.

I'm having a similar issue with Azure SDK here https://github.com/Azure/azure-sdk-for-net/issues/3312

If I comment the code that download a blob from Azure Blob, the error doesn't happen. If I put it back, it fail with that curl exception... Any clues would be appreciated...

@galvesribeiro, do you have a standalone repro you can share? What OS? What version of .NET Core? Etc. A repro targeting HttpClient directly rather than going through pounds of layers above it would be best. :smile:

I get the same error while running this piece of code in a console app

 using (HttpClient client = new HttpClient())
            {               
                var result = client.GetAsync("https://2.ubookrdev.com").Result;                
                Console.Write(result.Content.ReadAsStringAsync().Result);    
            }  

OS: Ubuntu
Dotnet Version 1.1

FYI @stephentoub : using http://google.com as URL for GetAsync method works properly and also using http://google.com1 will return a not found exception which is correct.

UPDATE 1:

I've checked the other versions of dotnet core which all have the same issue, here is the list of them:

1.0.3
1.0.4
dotnet-dev-1.1.0-preview1-005051
dotnet-dev-2.0.0-preview1-005977

Same problem on WSL (Bash on Windows) with .NET Core 2.0.
I have an ASP.Net Core http server running in one bash, and try to connect with a Core console app in another bash, and get the error.

My workaround:

  • Do not listen on http://+:port but on http://specific-ip:port.
  • Connect to http://specific-ip:port from the client app.

@patroza : Yes, as you said mentioning "IP Address : specific port" works !

Was this page helpful?
0 / 5 - 0 ratings