2013-03-07 49 views
0

我正在嘗試使用HTTPWebRequest登錄到一個站點,它工作正常。問題是,即使我傳遞了錯誤的憑證,或者錯誤的URL,它也能正常工作。沒有例外被拋出。爲什麼WebRequest使用無效的URL?

我應該怎麼做才能在下面的代碼中得到異常?

try 
{ 
    WebRequest request = WebRequest.Create("http://192.1111.1.1111"); 
    ViewBag.Message = request.ContentLength; 
} 
catch (Exception e) 
{ 
    ViewBag.Message = "failed"; 
} 

或者還有其他解決方法嗎?

回答

4

您實際上沒有提出請求,只是創建了類。爲了提出請求,您需要閱讀回覆。您也可以通過訪問請求流來發起請求。

//Create a new WebRequest Object to the mentioned URL. 
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com"); 

// This will throw an exception if the request fails 
WebResponse myWebResponse=myWebRequest.GetResponse(); 
2

你沒有這個代碼執行請求,你必須補充一點:

try 
{ 
    WebRequest request = WebRequest.Create("http://192.1111.1.1111"); 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    ViewBag.Message = response.ContentLength; 
} 
catch (WebException e) 
{ 
    ViewBag.Message = "failed"; 
} 

後,除可以檢查Status code(401未經授權等)

0

您需要實際提出請求,而不僅僅是創建類。

你可以得到類似的迴應:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

0

您提供的代碼示例只是初始化請求對象。您提供給請求的參數被認爲是有效的,因此不會引發異常。

當您進行連接時,可能會引發錯誤。

請參閱MSDN docs on WebRequest.Create()瞭解有關異常情況以及何時引發的更多信息。

0

thankx人...我研究了你的答案,是的,它的工作...我能夠登錄到Facebook的一些有效的憑據...多數民衆贊成在偉大的! :)下面是我的最終代碼: -

 CookieCollection cookies = new CookieCollection(); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.facebook.com"); 
     request.CookieContainer = new CookieContainer(); 
     request.CookieContainer.Add(cookies); 
     //Get the response from the server and save the cookies from the first request.. 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     cookies = response.Cookies; 



     string getUrl = "https://www.facebook.com/login.php?login_attempt=1"; 
     string postData = String.Format("email={0}&pass={1}", "", ""); 
     HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); 
     getRequest.CookieContainer = new CookieContainer(); 
     getRequest.CookieContainer.Add(cookies); //recover cookies First request 
     getRequest.Method = WebRequestMethods.Http.Post; 
     getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; 
     getRequest.AllowWriteStreamBuffering = true; 
     getRequest.ProtocolVersion = HttpVersion.Version11; 
     getRequest.AllowAutoRedirect = true; 
     getRequest.ContentType = "application/x-www-form-urlencoded"; 

     byte[] byteArray = Encoding.ASCII.GetBytes(postData); 
     getRequest.ContentLength = byteArray.Length; 
     Stream newStream = getRequest.GetRequestStream(); //open connection 
     newStream.Write(byteArray, 0, byteArray.Length); // Send the data. 
     newStream.Close(); 

     HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse(); 
     using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) 
     { 
      string sourceCode = sr.ReadToEnd(); 
      ViewBag.Message = sourceCode; 

     } 

Ofcourse,還有許多其他的東西,在代碼除了HttpWebRequest和httpwebresponse ...
1)你必須運行的HttpWebRequest兩次,第一次拿到餅乾收集,然後使用相同的cookiecollection實際登錄coz臉書登錄前設置某些cookie,它需要在登錄時提取...
2)(這是我早先的問題的答案,即如何檢查是否URL正在工作),所以你要做的是獲得一個字符串中的頁面內容(在這種情況下,sourceCode)n然後只搜索一個特定的字符串,你認爲可能在那裏在結果頁...就像在這種情況下,我可以可能是紅色的如果我已經成功登錄,就定向到FB上用戶的主頁。那個頁面可能有用戶名或者頁面標題爲「Facebook」等等......等等......所以我們如何檢查成功的網址有或沒有憑據....

多數民衆贊成它...這件簡單的事情花了我三天差不多...... !!!荒謬!

+0

現在我有另一個問題,你可以使用相同的代碼n做oauth認證....我的意思是我有一個oauth URL給我的人和用戶名和密碼Facebook。現在我嘗試更換上面的代碼中的字符串,但它沒有任何用處...我google'd,OFCOURSE ...,它給了我所有其他方式...但我想知道如果我詛咒使用相同的代碼進行一些修改...你可以建議任何網址,所以我可以在那裏研究...在此先感謝 – 2013-03-08 19:27:34