troubleshooting Question

HttpWebRequest how to inject Cookies?

Avatar of adworldmedia
adworldmediaFlag for United States of America asked on
.NET ProgrammingC#ASP.NET
4 Comments1 Solution756 ViewsLast Modified:
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();
ASKER CERTIFIED SOLUTION
adworldmedia
CTO

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros