Avatar of adworldmedia
adworldmediaFlag 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);
        try
        {
            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("</font></p>");                
            }
            else
                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);
        saveCookie(cookies);
        Stream data = response.GetResponseStream();

Open in new window

C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
adworldmedia
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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.
Avatar of ripahoratiu
ripahoratiu
Flag of Romania image

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.
ASKER CERTIFIED SOLUTION
Avatar of adworldmedia
adworldmedia
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of adworldmedia
adworldmedia
Flag of United States of America image

ASKER

Solved it before anyone else gave a solution
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo