我想谷歌对用户有一个限制,所以用户必须登录才能下载文件,我想用http post登录到像谷歌这样的网站,然后下载文件。
如何使用http POST登录类似google的站点?
发布于 2011-05-20 19:08:14
我建议使用一些pop或imap组件来检索邮件。例如Open Pop .NET。
发布于 2011-05-20 18:56:42
如果不提供有关此站点如何处理身份验证的更多详细信息,则无法回答您的问题。仅仅说像google这样的网站是不够的。例如,Google提供了一个API来实现这一点。
现在,让我们假设这个站点使用cookie来跟踪经过身份验证的用户。下面是所涉及的过程的概述。您可以使用HttpWebRequest的CookieContainer属性。因此,您将向页面发送第一个请求,允许通过发送用户名/密码进行身份验证。然后,cookie容器将捕获身份验证cookie,并在后续请求下载文件时发送该cookie。
和带代码的插图:
var container = new CookieContainer();
var request = (HttpWebRequest)WebRequest.Create("https://example.com/login");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = container;
using (var stream = request.GetRequestStream())
using (var writer = new StreamWriter(stream))
{
var values = HttpUtility.ParseQueryString(string.Empty);
values["password"] = "secret";
values["username"] = "someuser";
writer.Write(values.ToString());
}
using (var response = request.GetResponse())
{
// At this stage if authentication went fine the
// cookie container should have the authentication cookie
// allowing to track the user
}
// Now let's send a second request to download the file
request = (HttpWebRequest)WebRequest.Create("https://example.com/authenticated_resource");
request.CookieContainer = container;
request.Method = "GET";
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
{
// TODO: do something with the response
}发布于 2011-05-20 22:49:35
这叫做屏幕抓取。您必须发出HTTP请求,在需要提交表单的情况下发布表单数据,然后解析响应。我使用HtmlAgilityPack使这些任务变得更容易,但它不会为您完成所有这些任务……
在这里看一下:
http://crazorsharp.blogspot.com/2009/06/c-html-screen-scraping-part-1.html
http://crazorsharp.blogspot.com/2009/06/c-html-screen-scraping-part-2.html
https://stackoverflow.com/questions/6070840
复制相似问题