Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:16:34+00:00 2026-06-04T01:16:34+00:00

I’m trying to do some automated web requests and need to maintain a cookie

  • 0

I’m trying to do some automated web requests and need to maintain a cookie from one to the next. I can see that I’m getting back the cookie I want from the initial response, but I can’t attach it to the next request.

c# code

// response part

using (var wresp = (System.Net.HttpWebResponse)wrequest.GetResponse())
{

    // respblob gets returned and is accessible to the next request
    respblob.CookieList = new List<System.Net.Cookie>(); 

    foreach (System.Net.Cookie cook in wresp.Cookies)
    {
        respblob.CookieList.Add(cook);
    }

    // ... more stuff not related to cookies
}


// next request part
   var wrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

    wrequest.Method = "POST";
    wrequest.ContentType = "application/x-www-form-urlencoded";

    wrequest.CookieContainer = new System.Net.CookieContainer();

    // request.CookieList contains one cookie as expected 
    // from the previous response
    for (int j = 0; j < request.CookieList.Count; j++)
    {
        wrequest.CookieContainer.Add(request.CookieList[j]);
    }


// .... write data to request body

// ... complete the request, etc

here is a recorded exchange for the two request/response actions.

request:

GET http://domain.com/Login.aspx?ReturnUrl=%2fDefault.aspx HTTP/1.1
Host: domain.com
Connection: Keep-Alive

response:

HTTP/1.1 200 OK
Date: Tue, 15 May 2012 17:17:52 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=zzz4fpb4alwi1du2yavx5tah; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 24408



...html content...

next request:

POST http://domain.com/Login.aspx?ReturnUrl=%2fDefault.aspx HTTP/1.1
Host: domain.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 979
Expect: 100-continue

__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=viewstateclipped&__EVENTVALIDATION=validationclipped&ctl00%2524ContentPlaceHolder1%2524Login1%2524LoginButton=&ctl00%2524ContentPlaceHolder1%2524Login1%2524UserName=my.email%40example.com&ctl00%2524ContentPlaceHolder1%2524Login1%2524Password=myPassword

So even though the cookie exists in the HttpWebRequest CookieContainer, it doesn’t get sent with the request. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:16:36+00:00Added an answer on June 4, 2026 at 1:16 am

    You should use the same CookieContainer instance for the both HttpWebRequest objects that you are using. simply create a CookieContainer instance once:

    var cookieContainer = new CookieContainer();
    

    and then have your both request objects use this instance:

    var request1 = (HttpWebRequest)WebRequest.Create("http://example.com/url1");
    // assign the cookie container for the first request
    request1.CookieContainer = cookieContainer;
    ... go ahead and send the request and process the response
    
    
    var request2 = (HttpWebRequest)WebRequest.Create("http://example.com/url2");
    // reuse the same cookie container instance as the first request
    request2.CookieContainer = cookieContainer;
    ... go ahead and send the request and process the response
    

    Since you are using the same CookieContainer for the both requests, when the first request stores the cookie in this container, the cookie will be emitted alongside the second request automatically. This of course assumes that the second request is to the same domain as the first one.

    Also since the cookie is a session cookie (HttpOnly flag) you cannot read its value from the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.