为什么 HttpWebRequest 第一次失败然后就正常了?

发布于 2024-10-15 02:24:52 字数 2828 浏览 7 评论 0原文

我正在努力将fusemail集成到asp.net 2.0中。我正在使用 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配置有问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-10-22 02:24:52

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

我希望这对你有帮助。

此致,

Mohamed Kamal Elbeah

高级 .Net 开发人员

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

不回头走下去 2024-10-22 02:24:52

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文