I`m building an application that need to authenticate session on a website and then submits/retrieved data.
This is what I`ve been able to do till now (through googling). I`ve been able to detect if the requested page actually exists or not, but more than that, I couldn`t proceed!
WebRequest request = WebRequest.Create("http://localhost/");
try
{
using (WebResponse response = request.GetResponse())
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
textBlock1.Text = "Connected ...\n Status code is: " + httpResponse.StatusCode;
}
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
textBlock1.Text = "Error Code: " + httpResponse.StatusCode;
}
}
This code just detects if the web source requested exists or not. Now I`ll assume that the source exists, how can I send username/password to be authenticated?
I`ll assume that the username/password fields are named user/pass simultaneously ... How can I send the username and password to the website to validate and read the response?
I believe that this part will be very helpful throughout the whole development process.
Have a look at this SO question (and answers): Login and retrieve data from embedded form page using C#
You only need to adjust the names of the form fields accordingly to your target site.
There is a Credentials property for the WebRequest object which you can use to send along with the request.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url-here");
request.Credentials = new NetworkCredential("username", "password");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.