使用http POST登录谷歌

时间:2011-05-20 10:51:41

标签: c# asp.net

认为google对用户有限制,因此用户必须登录才能下载文件,我想使用http post登录到google等网站,然后再下载文件。

如何使用http POST登录google网站?

3 个答案:

答案 0 :(得分:1)

如果没有提供有关此网站如何处理身份验证的更多详细信息,您的问题就无法回答。只是说像谷歌这样的网站是不够的。例如,Google提供了执行此操作的API。

现在让我们假设这个站点使用cookie来跟踪经过身份验证的用户。以下是相关流程的概述。您可以使用HttpWebRequest的CookieContainer属性。因此,您将向页面发送第一个请求,允许通过发送用户名/密码进行身份验证。然后,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
}

答案 1 :(得分:0)

我建议使用一些pop或imap组件来检索邮件。例如Open Pop .NET

答案 2 :(得分:0)

这叫做屏幕抓取。您必须发出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