httpwebrequest not getting all cookies

Posted on 2009-06-29
Last Modified: 2013-11-27
I'm trying to log into Gmail using httpwebrequest
Login Page:

Cookies Example:

Name      NID
Value      24=qt-xxhhTTzY6EsgcHEsEJLbV4g0Ak3EYcVs-zvIkaLK8J-0vw3PdfHIP9sovL7V-ayQ_AlUgSo_5TG9_clYq0_zZrLC9Nmbo_jkyu645czYrs03eenI6PfA8IFt9EKba
Path      /
Secure      No
Expires      Wed, 30 Dec 2009 04:25:17 GMT

Name      PREF
Value      ID=7a29976d0736ce96:U=db98138f09065e00:TM=1245168329:LM=1245174280:GM=1:S=Ga8mZhXB65Ap0ram
Path      /
Secure      No
Expires      Thu, 16 Jun 2011 17:44:39 GMT

Name      __utma
Value      173272373.928349011.1245168415.1246069676.1246206632.33
Path      /accounts/
Secure      No
Expires      Tue, 28 Jun 2011 16:30:31 GMT

Name      __utmz
Value      173272373.1245168415.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
Path      /accounts/
Secure      No
Expires      Wed, 16 Dec 2009 04:06:54 GMT

Name      rememberme
Value      false
Path      /
Secure      No
Expires      Mon, 24 Jun 2019 14:13:21 GMT

Name      GALX
Value      bdTHhQZCu54
Path      /accounts
Secure      Yes
Expires      At End Of Session

I run the code below and the only cookie it gets is the GALX
Dim CookieJar = New CookieContainer
 Dim httpRequest As HttpWebRequest
 Dim httpResponse As HttpWebResponse
 Dim responseStream As Stream
 Dim responseEncoding As Encoding
 Dim responseStreamReader As StreamReader
 Dim strResponse1 As String = ""
CookieJar = New CookieContainer
httpRequest = CType(WebRequest.Create(""), HttpWebRequest)
httpRequest.Timeout = 5 * 10000
httpRequest.AllowAutoRedirect = True
httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
httpRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)"
httpRequest.KeepAlive = True
httpRequest.CookieContainer = CookieJar
httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
responseStream = httpResponse.GetResponseStream()
responseEncoding = System.Text.Encoding.GetEncoding("utf-8")
responseStreamReader = New StreamReader(responseStream, responseEncoding)
If Not responseStreamReader Is Nothing Then
    strResponse1 = responseStreamReader.ReadToEnd
End If

Open in new window

Question by:neta1o
  • 3
  • 2
LVL 39

Expert Comment

ID: 24743968
It may be a lot easier if you use the .NET GMail API for your task:

The problem with the request is probably the domain name. Are the above cookies inside your CookieJar? Can you change the hosts to reflect "" instead of ""?

Note that the rememberme cookie is expired.

The server is not required to bounce the cookies back. The server is, in general, in control of the cookies. As an example, it could be that if they consider you're not from Internet Explorer, that certain cookies are not valid. In which case it may simply ignore some of them. In this case you mimic Firefox, but I'm not sure if all headers are set correctly to fool the server.

All these and other problems have already been resolved in the API I mentioned above.To save yourself a lot of problems, I can strongly suggest giving it a shot.

-- Abel --
LVL 39

Expert Comment

ID: 24743984
> Are the above cookies inside your CookieJar?
hmm, I can answer that myself. Looking at your code again, I see that there are not cookies at all in the cookiejar, or perhaps you left out some of the code? Getting a meager response is then quite likely. If you want to send the cookies to the server to mimic a login of something, you'll have to send the cookies along, they're not picked up from somewhere automatically...

Author Comment

ID: 24744404
The code above is a snippet, I have the code for the POST part that uses the same cookiejar but to keep the code simple I wanted to use that example.  It's also not a Gmail issue.  If you try to do a GET on

You will also not get the cookie.

The only commonality I see is that it is a secure site (https) and the web request only seems to find the cookies marked as secure: yes as indicated by web developer toolbar (pasted above from the gmail example)
LVL 39

Expert Comment

ID: 24746046
If I do a GET with my browser I don't see any cookies either for that site so that's not too surprising.

> and the web request only seems to find the cookies marked as secure
I noticed the "s" but didn't make that connection. But you are right of course. Can't believe I missed that, must be tired over here or something. You just answered the question ;-)

Accepted Solution

neta1o earned 0 total points
ID: 24747087
My gut feeling is it has to do with the secure site and the non secure cookie.

For Example:
If I do a httpwebrequest on that page with a cookie container I don't get any cookies.

Although if I go to that site with IE or FIrefox, with or without Javascript enabled, the cookie is passed to my computer.  There has to be a way to capture this cookie.


Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question