为什么httpwebrequest首次失败,然后正常工作?
-
25-10-2019 - |
题
我正在努力将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,网站在IIS 7上托管了Windows Server 2008标准。另外,它首次失败了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服务器 /防火墙设置,这是网络配置问题。您必须联系您的网络管理员以检查此问题。
我希望这对您有帮助。
你的,
穆罕默德·卡马尔·埃尔贝(Mohamed Kamal Elbeah)
高级.NET开发人员
不隶属于 StackOverflow