时间:2019-03-17 标签:c#httpwebrequestcredentials问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题:
您在协议级别提供凭据,这不是大多数网站(包括这个网站)的工作方式。该协议是完全匿名的,并且该站点使用表单身份验证来登录。您的代码实际上需要创建一个模仿提交登录表单的 POST 请求。从服务器返回的响应将包含一个包含您的身份验证令牌的 cookie,这会导致...
您需要在请求之间保留 cookie。提交登录请求并获取 cookie 后,您需要保留它并将其在每个后续请求的请求标头中发送。最简单的方法是使用 WebClient 用于跨越多个请求,以及 CookieContainer 来为您跟踪 cookie。
如果您不确定如何模拟浏览器和网站之间的流量,可以使用一个很好的工具
You have two problems here:
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...
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.