1

我正在尝试将 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: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机未能响应 67.207.202.118:80

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

IIS配置有什么问题吗?

4

2 回答 2

1

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

我希望这对你有所帮助。

你的,

穆罕默德·卡迈勒·艾尔比亚

高级 .Net 开发人员

于 2011-04-17T15:01:47.917 回答
1

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

于 2019-12-18T22:33:01.717 回答