2011-01-27 293 views
0

我正在修整fusemail在asp.net 2.0中。我使用HttpWebRequest來請求API頁面。最近我發現HttpWebRequest第一次失敗,然後繼續並且後續請求成功。爲什麼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上。此外查驗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配置有什麼問題嗎?

+1

不知道。如果您的ping失敗,那麼它指向您環境中的網絡問題。您可以嘗試創建system.net跟蹤日誌(請參閱http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html)並查看日誌文件以瞭解HWR在網絡層看到的內容。 – feroze 2011-01-27 18:02:11

+0

從命令行嘗試telnet 67.207.202.118 80,查看是否可以在端口80上打開機器的套接字。另外,您粘貼的ping跟蹤第一次嘗試了不同的ip-192.168.0.253? – 2011-02-23 11:35:49

回答

1

這不是一個編程問題,我後來遇到了類似的問題,這是由於ISA服務器/防火牆設置導致的網絡配置問題。 你必須聯繫你的網絡管理員來檢查這個問題。

我希望這對你有所幫助。

你的,

穆罕默德·卡邁勒Elbeah

高級.NET開發人員

相關問題