简体   繁体   中英

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

I am having an issue:

A connection 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 URL.

Also I get issue like:

System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host

and

System.Net.WebException: The operation has timed out at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)

I know that this question has asked before but believe me I tried almost every single resolution at my end.

Weird thing is this issue does not appear at my end at all and only when client try to use which is not constant. Some time my application works fine at their end too.

What I am trying to accomplish is I have created an desktop application (Win Forms & C#) and trying to login client while sending request to my php api server which is hosted on GODaddy.

var request = (HttpWebRequest)WebRequest.Create("url");
if (request != null)
{
    #region System.Net.WebException
    request.Method = "POST";
    if (!string.IsNullOrEmpty(body))
    {
        var requestBody = Encoding.UTF8.GetBytes(body);
        request.ContentLength = requestBody.Length;
        request.ContentType = "application/json";
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(requestBody, 0, requestBody.Length);
        }
    }
    else
    {
        request.ContentLength = 0;
    }
    request.Timeout = 15000;
    request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);

    string output = string.Empty;
    try
    {
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1252)))
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    while (!stream.EndOfStream)
                    {
                        output += stream.ReadLine();
                    }
                    output = stream.ReadToEnd();
                }
            }
        }
    }
    catch
    {
        // Excaption Caught here
    }
}

Please help me out.

Thanks in advance.

Try adding this...

request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.ServicePoint.ConnectionLimit = 12;

MSDN article recommends 12 connections per CPU hence the limit of 12 because it's hosted by GoDaddy so I'm sure the server resources are limited.

I think I got the answer. Thought to post it so that it could help anyone else.

It most probably the particular machine issue or server issue but in my case, I tried to set Request.proxy = null and it worked like a charm.

I faced this problem at today. I set Timeout property of HttpWebRequest to 1 200 000 milliseconds (20 minute), but I got this error after 5 minute. I researched many sites at google and I found ReadWriteTimeout property of HttpWebRequest. Default value of ReadWriteTimeout is 5 minute. I increased value of ReadWriteTimeout and problem solved for me.

...

httpWebRequest.Timeout = 1200000;
httpWebRequest.ReadWriteTimeout = 1200000;

...

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.

Related Question A connection attempt failed because the connected party did not properly respond after a period of time in Azure How to fix “a connection attempt failed because the connected party did not properly respond after a period of time …” error? SocketException: A connection attempt failed because the connected party did not properly respond after a period of time on Windows 7 SP 1 SmtpClient: A connection attempt failed because the connected party did not properly respond after a period of time “A connection attempt failed because the connected party did not properly respond after a period of time” using WebClient A connection attempt failed because the connected party did not properly respond after a period of time or connected host has failed to respond A connection attempt failed because the connected party did not properly respond after "A connection attempt failed because the connected party did not properly respond after a period of time" using HTTPClient for calling 3rd party url System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time A connection attempt failed because the connected party did not properly respond after a period of time - having issue only in aws server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM