How to login to HTML form using POST vars in C# (.NET)?

ⅰ亾dé卋堺 提交于 2019-12-30 11:26:31

问题


For example, there is this website: www.azet.sk

On the right, there is login and password, I'd like my application to login to this web application and retrieve the data from my own account to C# (.NET) application and work with it. The aim is to keep the "logged in" connection alive and send vars using POST method. Is there any tutorial or easy script with examples to learn this?


回答1:


string username = "your";
string password = "password";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://moje.azet.sk/prihlasenie.phtml?KDE=www.azet.sk%2Findex.phtml%3F");
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Retrieve your cookie that id's your session
//response.Cookies

using (StreamReader reader = new StreamReader(response.GetResponseStream())
{
    Console.WriteLine(reader.ReadToEnd());
}



回答2:


The login part should be relatively easy.

Use System.Net.WebClient and the UploadValues() method to POST the form data. Look at the HTML source to figure out the field values to POST.

Most forms-based auth mechanisms use an HTTP cookie to keep the user logged in. So, after you POST the values, examine the WebClient's ResponseHeaders collection for the 'Set-Cookie:' header.

Store the cookie value for subsequent GETs/POSTs to the website.

You'll probably find that "retrieving the data from your account and working with it" is much more complicated (i.e. screen-scraping, etc).

Here's a link that has some examples:

http://codebetter.com/blogs/brendan.tompkins/archive/2005/05/18/63329.aspx




回答3:


Look into the WebBrowser control. You can navigate anywhere and access the document and its controls programatically.



来源:https://stackoverflow.com/questions/524525/how-to-login-to-html-form-using-post-vars-in-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!