Search code examples
c#.nethttpwebrequesthttpwebresponse

Get and Post using HttpWebRequest to log into website C# .Net


I created a c# console application to log into a website with Selenium and it works great. Now I want to add this feature to an existing .Net Core application.

So far I have the following:

string formUrl = "https://cap.mcmaster.ca/mcauth/login.jsp?app_id=1505&app_name=Avenue";
string formParams = string.Format("user_id={0}&pin={1}", "user", "pass"); //In my program I have the correct credentials
string cookieHeader;
var cookies = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
req.CookieContainer = cookies;
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"];


string pageSource;
string getUrl = "https://avenue.cllmcmaster.ca/d2l/home";
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(resp.cookies);
getRequest.Headers.Add("Cookie", cookieHeader);

getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
    pageSource = sr.ReadToEnd();
}

For some reason, in the above code Cookies is underlined in "resp.Cookies" as an error. (CS1061 'WebResponse' does not contain a definition for 'Cookies' and no accessible extension method 'Cookies' accepting a first argument of type 'WebResponse' could be found)

I tried to folow the posts here: Login to website, via C# but I can't seem to get it to work.


Solution

  • The reason you are getting a compile time error is because the WebResponse class does not have a Cookies property.

    From .NET Docs

    The WebResponse class is the abstract base class from which protocol-specific response classes are derived.

    Instead, take a look at the HttpWebResponse class. This class provides a HTTP specific implementation of WebResponse.

    You can cast the WebResponse to a HttpWebResponse:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();


    HttpWebResponse.Cookies Property

    Gets or sets the cookies that are associated with this response.

    Documentation

    HttpWebResponse.Cookies