質問

私は、通常のHTML応答と一緒に私のすべての要求に対して401認証エラーを送信し、サーバーへの接続を確立しようとしています。例えば しかし、私はまた、私はそれを解析することができるように伴って含む、送信されたHTMLレスポンスを読みたいです。 LiveHTTPHeadersを使用してキャプチャ例ヘッダ交換: 明らかに、コンテンツ長は非ゼロです。 Firefoxは、それはjavascriptのあることを示しています。

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();

例外をスローします。

私は私が実際のコンテンツに得るのを助けるために何か提案に感謝されます。

感謝します。

役に立ちましたか?

解決

あなたはこのように、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.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top