Why does HttpWebRequest fail the first time and then works OK?

3.4k views Asked by At

I am truing to integrate fusemail in asp.net 2.0. I am using HttpWebRequest for requesting the API pages. It has recently come to my notice that HttpWebRequest fails the first time and then continues and subsequent requests succeed.

say ( i know if i use goto it is a bad programming approach) if i use this code

retry:
        try
        {
            Uri uri = new Uri("http://www.fusemail.com/api/request.html");
            if (uri.Scheme == Uri.UriSchemeHttp)
            {

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
                request.PreAuthenticate = true;
                request.Method =
                WebRequestMethods.Http.Post;

                //request.ReadWriteTimeout = System.Threading.Timeout.Infinite;
                //request.Timeout = System.Threading.Timeout.Infinite;
                request.ContentLength = data.Length;
                request.ContentType =
                "application/x-www-form-urlencoded";
                //request.UserAgent = Request.UserAgent;
                request.UserAgent = "Mozilla/4.0";
                request.KeepAlive = false;

                request.ServicePoint.Expect100Continue = true;
                //request.Accept = "Accept: text/html,application/xhtml+xml,application/xml";


                StreamWriter writer = new StreamWriter(request.GetRequestStream());
                writer.Write(data);
                writer.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                StreamReader reader = new StreamReader(response.GetResponseStream());
                string tmp = reader.ReadToEnd();
                response.Close();
                //Response.Write(tmp);

                if (!String.IsNullOrEmpty(tmp))
                {
                    return tmp;
                }
            }
            return String.Empty;
        }
        catch (WebException ex)
        {

            goto retry;
        }

it works after failing once. i am writing to a text file in case of an error and after i failed request it works the second time. I am using ASP.Net 2.0 and the website is hosted on IIS 7 with Windows Server 2008 Standard. Also pinging the API address it fails the first time and then responds

C:\>ping 67.207.202.118

Pinging 67.207.202.118 with 32 bytes of data:
Reply from 192.168.0.253: **Destination host unreachable**.
Reply from 67.207.202.118: bytes=32 time=218ms TTL=49
Reply from 67.207.202.118: bytes=32 time=218ms TTL=49
Reply from 67.207.202.118: bytes=32 time=217ms TTL=49

Ping statistics for 67.207.202.118:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 217ms, Maximum = 218ms, Average = 217ms

The first time it fails in HttpWebRequest it fails with this error

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 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 67.207.202.118:80

Is there an authentication issue the first time?. i read on some forums it first sends a 401 Unauthorized and then it can connect. I am unable to verify this using Fiddler.

Is there anything wrong with IIS configuration?

2

There are 2 answers

1
Mohamed Kamal Elbeah On BEST ANSWER

This is not a programming issue at all, I have faced a similar problem later and it was a network configuration problem due to ISA server / Firewall settings. You have to contact your network administrator to check this issue.

I wish this helped you.

Yours,

Mohamed Kamal Elbeah

Senior .Net Developer

0
The Librarian On

I recently came by this same issue. The solution in my case involved my testing environment, since I had multiple Ethernet adapters connected to my computer. While you may have a different IP for each of your Ethernet adapters, if they are all assigned to the same subnet this may cause a problem. The TCP connection is only attempted using one NIC at a time. So in my case, on the first try it would attempt the connection on one adapter that was not connected to my remote host, then on the second try it would connect using the second adapter which was connected. - Hope this helps someone.