10

I have a winforms application i have been working on that runs multiple tests on consumer accounts. The tests require a one time login in order to execute.

string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
        string strResponse;
        HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
        requestLogin.Method = "POST";
        requestLogin.CookieContainer = cookieJar;
        requestLogin.ContentType = "application/x-www-form-urlencoded";

        requestLogin.ContentLength = paramaters.Length;
        StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(paramaters);
        stOut.Close();

        StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
        strResponse = stIn.ReadToEnd();
        stIn.Close();

This script works for the login just fine, the problem is when i need to actually run the tests i need to return all the results into a string (HTML results).

private string runTestRequest(Uri url, string parameters)
    {
        string testResults = string.Empty;
        HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
        runTest.CookieContainer = cookieJar;
        runTest.Method = "POST";
        runTest.ContentType = "application/x-www-form-urlencoded";
        StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
        stOut.Write(parameters);
        stOut.Close();
        StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
        testResults = stIn.ReadToEnd();
        stIn.Close();
        return testResults;
    }

But it goes and tries to login. How can i use the Cookie from the previous login request with this + many other web requests?

Thanks for the help.

EDIT:

I added this to my code yet which should do the same thing as BrokenGlass is saying except just a little different but still doesn't work.

foreach (Cookie cookie in responseLogin.Cookies)
        {
            cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
            richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
        }
Alex
  • 2,114
  • 9
  • 25
  • 34

1 Answers1

21

Something like this should work, I am using similar code to save a login cookie:

HttpWebRequest runTest;
//...do login request
//get cookies from response

CookieContainer myContainer = new CookieContainer();
for (int i = 0; i < Response.Cookies.Count; i++)
{
   HttpCookie http_cookie = Request.Cookies[i];
   Cookie cookie = new Cookie(http_cookie.Name, http_cookie.Value, http_cookie.Path);
   myContainer.Add(new Uri(Request.Url.ToString()), cookie);
}

//later:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.url.com/foobar");
request.CookieContainer = myContainer;
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • I don't think I understand how do implement your idea. The runTest request will be done after the initial login, and i dont know the names of the cookies. Can you please go into more detail? – Alex Nov 11 '10 at 19:54
  • @Alex: I revised my answer for http cookies - my initial response I adapted from use of SOAP service where you can make the login request and then get the cookies from the same variable – BrokenGlass Nov 11 '10 at 20:09
  • I'm not sure where exactly i would fit your code in with mine. I don't have a "Response" variable and my application doesn't understand what HttpCookie is. Thanks for the help again. – Alex Nov 11 '10 at 20:12
  • Alex: check your code: requestLogin.GetResponse() is the Response object you are looking for. For HttpCookie you have to add a reference to System.Web in your app. – BrokenGlass Nov 11 '10 at 20:25
  • Broken Glass, Check the OP I updated some code and basically did the what i think is basically the same thing and it still didnt work. – Alex Nov 11 '10 at 20:30
  • Where is `Request` from? (in `Request.Cookies[i]`) – vee Mar 10 '17 at 08:54