2010-01-10 248 views
0

我試圖建立一個連接到一個服務器,發送401身份驗證錯誤的所有我的請求以及正常的html響應。例如 但是,我也想閱讀隨同發送的HTML響應,以便我可以解析該響應。使用LiveHTTPHeaders捕獲示例頭交換:顯然,內容長度不爲零。 Firefox顯示它是JavaScript。獲取StatusCode 401的響應內容

https://172.31.1.251:1003/fgtauth?73e285357b2dc5cc 
Request: 
GET /fgtauth?73e285357b2dc5cc HTTP/1.1 
Host: 172.31.1.251:1003 
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b4) Gecko/20090423 Firefox/3.5b4 (.NET CLR 3.5.30729) 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 300 
Connection: keep-alive 

Response: 
HTTP/1.x 401 Unauthorized 
WWW-Authenticate: Fortigate 
Content-Length: 1091 
Connection: Keep-Alive 
Cache-Control: no-cache 
Content-Type: text/html 

在這一點上,形式在Firefox打開,要求我輸入我的用戶名和密碼。

https://172.31.1.251:1003/ 

Request: 
POST/HTTP/1.1 
Host: 172.31.1.251:1003 
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b4) Gecko/20090423 Firefox/3.5b4 (.NET CLR 3.5.30729) 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 300 
Connection: keep-alive 
Referer: https://172.31.1.251:1003/ 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 93 
magic=73e285357b2dc5cc&username=uuxx&password=xxuu&4Tredir=http%3A%2F%2Fwww.google.com%2F 


Response: 
HTTP/1.x 401 Unauthorized 
WWW-Authenticate: Fortigate 
Content-Length: 924 
Connection: Keep-Alive 
Cache-Control: no-cache 
Content-Type: text/html 

此時我被重定向到另一個URL。但是,我的問題是如何獲得與401未授權一起發送的長度爲924的內容,因爲該內容將幫助我進一步完成我想要做的事情。但是非常行:

WebResponse loginResponse = loginRequest.GetResponse(); 

引發異常。

我將不勝感激任何建議,以幫助我獲得實際內容。

謝謝。

+0

是否真正迴歸 「HTTP/1.x的」?這是違法的。 – EricLaw 2010-01-10 16:52:47

回答

4

你需要閱讀WebExceptionResponse財產,像這樣:

WebResponse loginResponse; 
try { 
    loginResponse = loginRequest.GetResponse(); 
} catch(WebException ex) { 
    if (ex.Status == WebExceptionStatus.ProtocolError) { 
     loginResponse = ex.Response; 
    } else 
     throw; 
} 

//Do something with the response, and remember to dispose it.