我正在尝试将 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配置有什么问题吗?