Asp.net 为什么HttpWebRequest第一次失败后工作正常?
我正在尝试将FuseEmail集成到asp.NET2.0中。我使用HttpWebRequest请求API页面。我最近注意到HttpWebRequest第一次失败,然后继续,后续请求成功 比如(我知道如果我使用goto,这是一种糟糕的编程方法)如果我使用这段代码Asp.net 为什么HttpWebRequest第一次失败后工作正常?,asp.net,httpwebrequest,system.net.webexception,Asp.net,Httpwebrequest,System.net.webexception,我正在尝试将FuseEmail集成到asp.NET2.0中。我使用HttpWebRequest请求API页面。我最近注意到HttpWebRequest第一次失败,然后继续,后续请求成功 比如(我知道如果我使用goto,这是一种糟糕的编程方法)如果我使用这段代码 retry: try { Uri uri = new Uri("http://www.fusemail.com/api/request.html"); if
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.NET2.0,该网站位于IIS7上,采用WindowsServer2008标准。同时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,然后再连接。我无法使用Fiddler验证这一点
IIS配置有什么问题吗?这根本不是编程问题,我后来遇到了类似的问题,这是由于ISA服务器/防火墙设置而导致的网络配置问题。 您必须与网络管理员联系以检查此问题 我希望这对你有帮助 你的 穆罕默德·卡迈勒·厄尔贝阿
高级.Net开发人员我最近也遇到了同样的问题。我的案例中的解决方案涉及到我的测试环境,因为我有多个以太网适配器连接到我的计算机。虽然每个以太网适配器的IP可能不同,但如果它们都分配到同一子网,则可能会导致问题。一次只能使用一个NIC尝试TCP连接。因此,在我的情况下,在第一次尝试时,它将在一个未连接到我的远程主机的适配器上尝试连接,然后在第二次尝试时,它将使用连接的第二个适配器进行连接。-希望这能帮助别人。不知道。如果ping失败,则表明您的环境中存在网络问题。您可以尝试创建system.net跟踪日志(请参阅)并查看日志文件,以查看HWR在网络层看到的内容。从命令行尝试telnet 67.207.202.118 80,以查看是否可以在端口80上打开计算机的套接字。此外,您粘贴的ping跟踪第一次尝试了不同的ip-192.168.0.253?您在防火墙上更改了什么设置?