问题
I want to post username and password to a login page of a remote website using asp.net and pass it, to access login-required pages on website. In the other words suppose there is a page on a website that i would to surf it an grab something from it ,but login is required before it . how to call that Login page and post username and password from the asp.net application to pass it.
Thanks in advance
回答1:
The comment with passing them as the query string only works for GET parameters. This works for POST. As you can see I use the form entries username, password and login (the button) but in your case it might be entirely different values. So use a tool like Fiddler to catch which values a normal login is sending. When I did something similar I had to use a cookie-aware webclient because the service used cookies for session values. The service you are trying to access could use a query-session string - it entirely depends on the service.
Another problem I ran into when doing this, was that I had to fetch a session id from the html page and sent it for logging it. I have not included the code for that here, but I have it if you need it :)
var client = new CookieAwareWebClient();
client.Encoding = Encoding.UTF8;
// Post values
var values = new NameValueCollection();
values.Add("username", someusername);
values.Add("password", somepassword);
values.Add("login", "Login"); //The button
// Logging in
client.UploadValues(loginPageUrl, values); // You may verify the result. It works with https :)
// Download some secret page
var html= client.DownloadString(someurl);
CookieAwareWebClient
public class CookieAwareWebClient : WebClient
{
private CookieContainer cookieContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = cookieContainer;
}
return request;
}
}
回答2:
try reading this -> http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/
if you use firefox - download "tamper-data" and "live http-headers" to see what information is passed to the login site
with live http-headers you can see which data is passed in the POST
回答3:
Lets suppose you want to redriect to www.ABC.com
construct querystring like
http://abc.com/products.aspx?field1=value1&field2=value2
And get this value on the other page. But it is recomended to use the POST method, hope this help
来源:https://stackoverflow.com/questions/5869785/post-username-and-password-to-login-page-programmatically