Avatar of adworldmedia
Flag for United States of America asked on

HttpWebRequest how to inject Cookies?

I am using the HttpWebRequest to get a URL.

But I want to be able to inject a cookie into that request; so when the code retrieves the webpage, the page its getting see's some cookies from the request.

I have not been able to figure this out.  So far my code looks like (but doesn't work):

        CookieContainer cookieJar = new CookieContainer();
        CookieCollection cookies = new CookieCollection();
        //WebRequest request = HttpWebRequest.Create(_url);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
            if (Request.Cookies.Count != 0)
                HttpCookie aCookie;
                request.CookieContainer = new CookieContainer();
                //HttpWebRequest cookieRequest = request as HttpWebRequest;
                sb.Append("<p><font color='blue'>Cookies Found (" + Request.Cookies.Count.ToString() + "): ");
                for(int i=0; i<Request.Cookies.Count; i++)
                    aCookie = Request.Cookies[i];
                    sb.Append("<BR>Name: " + aCookie.Name + "<BR>");
                    sb.Append("Value: " + aCookie.Value + "<BR>");
                    //cookieJar.Add(new Cookie(aCookie.Name, aCookie.Value, aCookie.Path, ".adultadworld.com"));  
                    request.CookieContainer.Add(new Uri("http://www.jpkphoto.info"), new Cookie(aCookie.Name, aCookie.Value));

                sb.Append("<P><font color='blue'>No Cookies Found</font></p>");
        catch (Exception ex)
            sb.Append("<P><font color='red'><b>Error Reading Cookies: " + ex.Message + "</b></font></p>");
        ((HttpWebRequest)request).UserAgent = _agent;
        ((HttpWebRequest)request).CookieContainer = cookieJar;
        WebResponse response = request.GetResponse();
        _cookie = cookieJar.GetCookieHeader(request.RequestUri);
        cookies = cookieJar.GetCookies(request.RequestUri);
        Stream data = response.GetResponseStream();

Open in new window

C#ASP.NET.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon
David Johnson, CD

Cookie Basics :http://msdn.microsoft.com/en-us/library/aa289495%28v=vs.71%29.aspx

Simply you have to create a cookie that the web browser gets and store in its cookie folder, then you can request the cookie to retrieve what you stored in the cookie.

Don't create CookieContainer more than once.
CookieContainer = new CookieContainer(); creates a new container, all the past cookies are lost on the client side.
Make it static or have an instance of it living while all your requests are performed.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Solved it before anyone else gave a solution
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy