从登录后的页面获取 HTML

发布于 2024-07-04 10:07:32 字数 450 浏览 5 评论 0原文

这个问题是我的上一个问题关于从 ASPX 页面获取 HTML 的后续问题。 我决定尝试使用 webclient 对象,但问题是我获取了登录页面的 HTML,因为需要登录。 我尝试使用 webclient 对象“登录”:

WebClient ww = new WebClient();

 ww.DownloadString("Login.aspx?UserName=&Password=");

 string html = ww.DownloadString("Internal.aspx");

但我仍然始终得到登录页面。 我知道用户名信息没有存储在 cookie 中。 我一定是做错了什么或者遗漏了重要的部分。 有谁知道它可能是什么?

This question is a follow up to my previous question about getting the HTML from an ASPX page. I decided to try using the webclient object, but the problem is that I get the login page's HTML because login is required. I tried "logging in" using the webclient object:

WebClient ww = new WebClient();

 ww.DownloadString("Login.aspx?UserName=&Password=");

 string html = ww.DownloadString("Internal.aspx");

But I still get the login page all the time. I know that the username info is not stored in a cookie. I must be doing something wrong or leaving out an important part. Does anyone know what it could be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我不会写诗 2024-07-11 10:07:33

使用 Fiddler 查看通过浏览器手动执行操作时发生的 HTTP 请求和响应。

Use Fiddler to see the HTTP requests and responses that happen when you do it manually through the browser.

成熟的代价 2024-07-11 10:07:33

@Fire Lancer:我在测试期间问了自己同样的问题,所以我检查了,它确实可以在浏览器中工作。

@Fire Lancer: I asked myself that same question during my tests, so I checked, and it does work from a browser.

转身以后 2024-07-11 10:07:33

由于我试图获取的 aspx 页面位于我自己的项目中,因此我可以使用 Server.Execute 方法。 更多详细信息请参阅我对原始问题的回答

As the aspx page I was trying to get was in my own projct, I could use the Server.Execute method. More details in my answer to my original question

浸婚纱 2024-07-11 10:07:33

将 Firefox 与 LiveHttpHeaders 插件结合使用。
这将允许您通过实际的浏览器登录并准确查看发送到服务器的内容。 我的第一个问题是验证它是否不需要表单中的 POST。 您正在加载的示例 URL 通过查询字符串 GET 发送信息。

Use Firefox with the LiveHttpHeaders plugin.
This will allow you to login via an actual browser and see EXACTLY what is being sent to the server. My first question would be to verify that it isn't expecting a POST from the form. The example URL you are loading is sending the info via a querystring GET.

彩虹直至黑白 2024-07-11 10:07:32

在浏览器中使用“Login.aspx?UserName=&Password=”打开页面是否正常工作?
某些页面可能不允许使用 url 中提供的数据进行登录,并且必须在页面的登录表单中输入该数据,然后提交。

Well does opening the page in a brower with "Login.aspx?UserName=&Password=" normaly work?
Some pages may not allow login using data provided in the url, and that it must be entered in the login form on the page and then submitted.

巨坚强 2024-07-11 10:07:32

只需将有效的登录参数传递给给定的 URI。 应该可以帮助你。

如果您没有登录信息,则不应尝试规避它。

public static string HttpPost( string URI, string Parameters )
      {
         System.Net.WebRequest req = System.Net.WebRequest.Create( URI );
         req.ContentType = "application/x-www-form-urlencoded";
         req.Method = "POST";
         byte[] bytes = System.Text.Encoding.ASCII.GetBytes( Parameters );
         req.ContentLength = bytes.Length;
         System.IO.Stream os = req.GetRequestStream();
         os.Write( bytes, 0, bytes.Length );
         os.Close();
         System.Net.WebResponse resp = req.GetResponse();
         if ( resp == null ) return null;
         System.IO.StreamReader sr = new System.IO.StreamReader( resp.GetResponseStream() );
         return sr.ReadToEnd().Trim();
      }

Just pass valid login parameters to a given URI. Should help you out.

If you don't have login information you shouldn't be trying to circumvent it.

public static string HttpPost( string URI, string Parameters )
      {
         System.Net.WebRequest req = System.Net.WebRequest.Create( URI );
         req.ContentType = "application/x-www-form-urlencoded";
         req.Method = "POST";
         byte[] bytes = System.Text.Encoding.ASCII.GetBytes( Parameters );
         req.ContentLength = bytes.Length;
         System.IO.Stream os = req.GetRequestStream();
         os.Write( bytes, 0, bytes.Length );
         os.Close();
         System.Net.WebResponse resp = req.GetResponse();
         if ( resp == null ) return null;
         System.IO.StreamReader sr = new System.IO.StreamReader( resp.GetResponseStream() );
         return sr.ReadToEnd().Trim();
      }
念﹏祤嫣 2024-07-11 10:07:32

尝试设置 WebClient 对象的凭据属性

WebClient ww = new WebClient();
ww.Credentials = CredentialCache.DefaultCredentials;
ww.DownloadString("Login.aspx?UserName=&Password=");
string html = ww.DownloadString("Internal.aspx");

Try setting the credentials property of the WebClient object

WebClient ww = new WebClient();
ww.Credentials = CredentialCache.DefaultCredentials;
ww.DownloadString("Login.aspx?UserName=&Password=");
string html = ww.DownloadString("Internal.aspx");
百善笑为先 2024-07-11 10:07:32

我能想到的唯一另一个原因是网页故意阻止其登录。如果您有权访问代码,请查看用于查看的登录系统,看看是否有任何旨在阻止此类登录的内容。

The only other reason I can think of then is that the web page is intentionally blocking it from loggin in. If you have access to the code, take a look at the loggin system used to see if theres anything designed to block such logins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文