So I am currently trying to log into my account on a website using WebRequest
.
I have been reading about it to the point where I feel like I wanted to use an example to learn by trial and error.
This is the example I am using Login to website, via C#
So when I try to execute my code it returns an unhandled exception and its this one
System.Net.WebException: 'The remote server returned an error: (404) Not Found.'
I tried stepping through the code and I THINK it might be that it's trying to POST somewhere where it can't. I wanted to fix this before moving onto getting a confirmation that it successfully logged in. I changed the username and password to dummy text for the sake of this question.
What did I do wrong here and whats the most logical way of fixing this issue? Thanks in advance.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string formUrl = "https://secure.runescape.com/m=weblogin/login.ws"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("login-username={0}&login-password={1}", "myUsername", "password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];