runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.Net.Http.CurlException HttpClient PostAsync

Open erikpowa opened this issue 10 years ago • 26 comments

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

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

var formContent = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("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.

erikpowa avatar Jun 11 '16 22:06 erikpowa

@ericeil can you take a look please?

joshfree avatar Jun 11 '16 22:06 joshfree

@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 avatar Jun 14 '16 12:06 stephentoub

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

joshfree avatar Jun 14 '16 13:06 joshfree

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

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

stephentoub avatar Jun 14 '16 13:06 stephentoub

@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.

joshfree avatar Jun 14 '16 14:06 joshfree

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.

stephentoub avatar Jun 14 '16 15:06 stephentoub

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.

erikpowa avatar Jun 14 '16 17:06 erikpowa

Moving to 1.1 based on current information

joshfree avatar Jun 14 '16 18:06 joshfree

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 avatar Jun 14 '16 18:06 erikpowa

@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 avatar Jun 14 '16 18:06 stephentoub

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

erikpowa avatar Jun 14 '16 18:06 erikpowa

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

stephentoub avatar Jun 14 '16 19:06 stephentoub

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.

ericeil avatar Jun 15 '16 20:06 ericeil

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 avatar Feb 07 '17 14:02 BlasVilla

@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!

stephentoub avatar Feb 07 '17 15:02 stephentoub

我在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

xumingxiang avatar Feb 12 '17 03:02 xumingxiang

@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();`

flo8 avatar Apr 09 '17 04:04 flo8

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

hach-que avatar Apr 09 '17 13:04 hach-que

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

KiarashS avatar May 06 '17 04:05 KiarashS

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.

iAlexanderMoon avatar May 17 '17 17:05 iAlexanderMoon