12
votes

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.

2
If it always works on your end and only sometimes on your client's end, it could be a firewall issue but it is http...do you use a different port for HttpWebRequests other than 80 & 443? Use pings to test the response times between your client and server as well to make sure they are not timing out or dropping packetswaltmagic
I don't use any port for my httpWebRequest. Also I can't use ping from my client's machine because I don't have access.Manish
That will make this hard to troubleshoot. The code looks OK and you said it does work for you and sometimes for them as well. I don't know how but you'll have to troubleshoot the network connections to make sure that is not the issue. Sorry I couldn't be of more help.waltmagic
@waltmagic, I know this is hard. I am also find it very difficult to debug. But what about HttpWebRequest port you are talking about? If its help you can see the inner exception which I get when any issue occurred at client's end: System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Send(Byte[] bufferManish
The default ports are 80 & 443 and most all firewalls allow those ports because those are reserved for http. If you could cause the error(get access to your clients' network) you could debug and step into your application until the code drops the connection and then you'd know what is going on.waltmagic

2 Answers

2
votes

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.

1
votes

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.