使用StatusCode 401获取响应的内容

编程入门 行业动态 更新时间:2024-10-25 11:21:24
本文介绍了使用StatusCode 401获取响应的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试建立与服务器的连接,该服务器为我的所有请求以及正常的html响应发送401身份验证错误.例如 但是,我也想阅读与之一起发送的HTML响应,以便我可以对其进行解析.使用LiveHTTPHeaders捕获的示例标头交换: 显然,content-length不为零. Firefox显示它是javascript.

I'm trying to establish a connection to a server that sends 401 Authentication Error for all my requests along with the normal html response. e.g. However, I also want to read the HTML response that is sent alongwith so that I can parse that. An example header exchange captured using LiveHTTPHeaders: Clearly, content-length is non-zero. Firefox shows it to be javascript.

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中打开一个表单,要求我输入用户名和密码.

At this point, a form opens in firefox that asks me to enter my username and password.

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: 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%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的内容,因为该内容将帮助我做我想做的进一步工作.但非常行:

At this point I am redirected to another URL. However, the problem that I have is how to get the content of lenght 924 that is sent along with the 401 Unauthorized, because that content will help me in doing what I want to do further. But the very line:

WebResponse loginResponse = loginRequest.GetResponse();

引发异常.

对于能帮助我了解实际内容的任何建议,我将不胜感激.

I will be grateful for any suggestions to help me get to the actual content.

谢谢.

推荐答案

您需要阅读WebException的Response属性,如下所示:

You need to read the WebException's Response property, like this:

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.

更多推荐

使用StatusCode 401获取响应的内容

本文发布于:2023-11-26 15:21:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   StatusCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!