为什么HttpWebRequest第一次失败然后工作正常?

时间:2011-01-27 07:42:44

标签: asp.net httpwebrequest system.net.webexception

我想在asp.net 2.0中集成fusemail。我正在使用HttpWebRequest来请求API页面。最近我注意到HttpWebRequest第一次失败然后继续,后续请求成功。

说(如果我使用这个代码

,我知道如果我使用goto这是一个糟糕的编程方法)
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;
        }

失败一次后才有效。我正在写一个文本文件,如果出现错误,在我请求失败后,它第二次工作。我正在使用ASP.Net 2.0,该网站托管在带有Windows Server 2008 Standard的IIS 7上。同时ping它的API地址第一次失败然后响应

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

第一次在HttpWebRequest中失败时,它失败并出现此错误

System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应67.207.202.118:80

第一次出现身份验证问题吗?我在一些论坛上看到它首先发送401 Unauthorized然后它可以连接。我无法使用Fiddler验证这一点。

IIS配置有什么问题吗?

2 个答案:

答案 0 :(得分:1)

我最近也遇到过同样的问题。就我而言,该解决方案涉及我的测试环境,因为我将多个以太网适配器连接到了我的计算机。尽管每个以太网适配器都有不同的IP,但是如果它们都分配给相同的子网,则可能会引起问题。一次仅使用一个NIC尝试TCP连接。因此,在我的情况下,第一次尝试将尝试在未连接到远程主机的一个适配器上进行连接,然后第二次尝试将使用已连接的第二个适配器进行连接。 -希望这对某人有帮助。

答案 1 :(得分:0)

这根本不是编程问题,我后来遇到了类似的问题,由于ISA服务器/防火墙设置,这是一个网络配置问题。 您必须与网络管理员联系以检查此问题。

我希望这对你有所帮助。

此致,

Mohamed Kamal Elbeah

高级.Net开发人员

相关问题