2013-01-31 377 views
0
public int loginEmail(string email, string password) 
    { 
     HttpWebRequest request = null; 
     string responseStr = null; 
     string Email = email; 
     string Pass = password; 

     UTF8Encoding encoding = new UTF8Encoding(); 
     string postData = "PostData"; 
     byte[] data = encoding.GetBytes(postData); 

     request = (HttpWebRequest)WebRequest.Create("url"); 
     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.AllowAutoRedirect = false; 
     request.KeepAlive = false; 
     request.Proxy = null; 
     request.ServicePoint.ConnectionLimit = 1000; 
     request.ContentLength = data.Length; 
     request.Timeout = 5000; 
     request.ServicePoint.ConnectionLeaseTimeout = 5000; 
     request.ServicePoint.MaxIdleTime = 5000; 

     using (Stream stream = request.GetRequestStream()) 
     { 
      stream.Write(data, 0, data.Length); 
     } 

     try 
     { 
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       responseStr = response.Headers["Set-Cookie"]; 
      } 
     } 
     catch 
     { 
      return 1; 
     } 

     string[] cooktemp; 
     string[] seperatortemp = new string[] { ";" }; 
     cooktemp = responseStr.Split(seperatortemp, StringSplitOptions.None); 

     LoginHeaders[0] = cooktemp[0] + ";"; 

     return 0; 
    } 

此代碼運行得很好,但有時請求沒有得到迴應。當請求沒有得到響應時,程序將掛起,然後最終會發出超時錯誤,導致程序崩潰。我現在想要做的只是捕捉超時錯誤,所以我可以處理它,但沒有任何東西似乎捕捉到它。捕捉HttpWebRequest超時

回答

4

這很可能是在GetRequestStream()超時。 documentation明確指出,如果請求的超時期限屆滿,它可能會拋出WebException。因此,在你的try/catch中包含這段代碼,你應該能夠抓住它。

-1

System.Net.WebException

try { ... }    
catch (System.Net.WebException sne) 
     { 
      MessageBox.Show(req.Timeout.ToString()); 
     } 

我認爲超時將永遠是 「5000」 不管是什麼。 如果您告訴它「超時時間爲5秒」,它會在放棄之前嘗試5秒。

0

這是一個古老的線索,但我今天也打了一個問題。

什麼我不知道的是,如果你有一個Web服務,比如說,嘗試寫入文件,該文件是鎖定 ...然後有一個簡單的try..catch的代碼是不夠的。

您必須特別有一個catch,其中處理WebExceptions

try 
{ 
    // Run your web service code 
} 
catch (WebException ex) 
{ 
    // Handle a WebException, such as trying to write to a "locked" file on the network 
} 
catch (Exception ex) 
{ 
    // Handle a regular Exception 
} 

我始終認爲,一個WebException是一個類型的Exception,所以這些會被這個catch處理程序被逮住:

catch (Exception ex) 
{ 
    // Handle a regular Exception 
} 

事實並非如此。

因此,爲避免您的代碼投擲「請求超時」消息,並沒有建議他們是什麼造成的,請記住添加這些第二個catch處理程序。

順便說一句,我的web services tutorial,這裏是我推薦的代碼,它看起來出了異常,並在響應頭部返回他們:

try 
{ 
    // Put your code in here 
} 
catch (WebException ex) 
{ 
    // Return any exception messages back to the Response header 
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; 
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError; 
    response.StatusDescription = ex.Message.Replace("\r\n", ""); 
    return null; 
} 
catch (Exception ex) 
{ 
    // Return any exception messages back to the Response header 
    OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; 
    response.StatusCode = System.Net.HttpStatusCode.InternalServerError; 
    response.StatusDescription = ex.Message.Replace("\r\n", ""); 
    return null; 
}