时间:2019-03-17 标签:c#httpwebrequestcredentials问题

发布于 2024-10-20 23:52:48 字数 1003 浏览 2 评论 0原文

我正在尝试使用 httpwebrequest 对象登录 www.diary.com 。然而,它总是无法登录,并且一直给我返回登录页面。谁能告诉我什么是错误的?

我的代码如下:

// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
    WebRequest.Create(@"http://diary.com/events/agenda");

request.ContentType = "text/html";

request.Credentials = new NetworkCredential(@"[email protected]", "password");

request.AllowAutoRedirect = true;
request.Referer = @"http://diary.com/";

// execute the request
HttpWebResponse response = (HttpWebResponse)
    request.GetResponse();

// we will read data via the response stream
Stream resStream = response.GetResponseStream();

// set the WebBrowser object documentStream to the response stream
myWB.DocumentStream = resStream;

// simply tell me the title of the webpage
MessageBox.Show(myWB.Document.Title);

I am trying to login into www.diary.com using a httpwebrequest object. However, it always fail to login, and kept giving me back the login page. Can anyone enlighten me on what is/are wrong?

My code is as follows:

// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
    WebRequest.Create(@"http://diary.com/events/agenda");

request.ContentType = "text/html";

request.Credentials = new NetworkCredential(@"[email protected]", "password");

request.AllowAutoRedirect = true;
request.Referer = @"http://diary.com/";

// execute the request
HttpWebResponse response = (HttpWebResponse)
    request.GetResponse();

// we will read data via the response stream
Stream resStream = response.GetResponseStream();

// set the WebBrowser object documentStream to the response stream
myWB.DocumentStream = resStream;

// simply tell me the title of the webpage
MessageBox.Show(myWB.Document.Title);

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

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

发布评论

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

评论(1

南烟 2024-10-27 23:52:49

这里有两个问题:

  1. 您在协议级别提供凭据,这不是大多数网站(包括这个网站)的工作方式。该协议是完全匿名的,并且该站点使用表单身份验证来登录。您的代码实际上需要创建一个模仿提交登录表单的 POST 请求。从服务器返回的响应将包含一个包含您的身份验证令牌的 cookie,这会导致...

  2. 您需要在请求之间保留 cookie。提交登录请求并获取 cookie 后,您需要保留它并将其在每个后续请求的请求标头中发送。最简单的方法是使用 WebClient 用于跨越多个请求,以及 CookieContainer 来为您跟踪 cookie。

如果您不确定如何模拟浏览器和网站之间的流量,可以使用一个很好的工具

You have two problems here:

  1. You are providing credentials at the protocol level, which is not how most websites (including this one) work. The protocol is totally anonymous, and the site uses Forms Authentication to log you in. Your code needs to actually create a POST request that mimics submitting the login form. The response that comes back from the server will include a cookie that has your auth token, which leads into...

  2. You need to persist cookies across requests. After you submit the login request and get the cookie, you'll need to hang on to it and send it along in the request headers of each subsequent request. The easiest way to do this is to use a WebClient for spanning multiple requests, and a CookieContainer to track the cookies for you.

If you're ever unsure about how to mimic the traffic that goes between your browser and a website, a great tool to use is Fiddler. It captures the raw request/response for you to observe.

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