Think google have 开发者_StackOverflowa limitation for user , so users have to login to download a file , I want to login to a site like google with http post and after that download a file .
how to login to site like google with http POST ?
Without providing more details about how this site handles authentication your question is impossible to answer. Just saying a site like google is not enough. For example Google provides an API for doing this.
Now let's imagine that this site uses cookies to track authenticated users. Here's an overview of the process involved. You could use the CookieContainer property of a HttpWebRequest. So you will send a first request to the page allowing to authenticate by sending the username/password. The cookie container will then capture the authentication cookie and it will be sent on subsequent request to download the file.
And illustration with code:
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
}
I would suggest to use some pop or imap component to retrieve mail. For example Open Pop .NET.
It's called screen scraping. You have to issue HTTP Requests, post form data in cases where you want to submit a form, and then parse the responses. I use HtmlAgilityPack to make these tasks easier, but it won't do it all for you...
Have a look here:
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
精彩评论