0

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.

Ryan H.
  • 2,543
  • 1
  • 15
  • 24
wf43fver
  • 89
  • 1
  • 2
  • 13
  • You should also look at the date of an answer. All that Setting/Getting Cookies is not necessary. When you add a `CookieContainer` to an `HttWebRequest`, that container is automatically updated when you receive a `WebResponse`. If you need to preserve the `CookieContainer` content, define one at class scope and set it as the `HttpWebRequest.CookieContainer` property when you create a new `WebRequest`. The new `WebRequest` will have all the previously exchanged Cookies. – Jimi Sep 29 '18 at 00:17

1 Answers1

0

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

Ryan H.
  • 2,543
  • 1
  • 15
  • 24