Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


httpwebrequest not getting all cookies

Posted on 2009-06-29
Medium Priority
Last Modified: 2013-11-27
I'm trying to log into Gmail using httpwebrequest
Login Page: https://www.google.com/accounts/ServiceLogin?service=mail

Cookies Example:

Name      NID
Value      24=qt-xxhhTTzY6EsgcHEsEJLbV4g0Ak3EYcVs-zvIkaLK8J-0vw3PdfHIP9sovL7V-ayQ_AlUgSo_5TG9_clYq0_zZrLC9Nmbo_jkyu645czYrs03eenI6PfA8IFt9EKba
Host      .google.com
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
Host      .google.com
Path      /
Secure      No
Expires      Thu, 16 Jun 2011 17:44:39 GMT

Name      __utma
Value      173272373.928349011.1245168415.1246069676.1246206632.33
Host      .google.com
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)
Host      .google.com
Path      /accounts/
Secure      No
Expires      Wed, 16 Dec 2009 04:06:54 GMT

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

Name      GALX
Value      bdTHhQZCu54
Host      www.google.com
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("https://www.google.com/accounts/ServiceLogin?service=mail"), 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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 39

Expert Comment

ID: 24743968
It may be a lot easier if you use the .NET GMail API for your task: http://sourceforge.net/projects/gmail-api

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

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 https://accounts.craigslist.org/

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: https://accounts.craigslist.org/
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Simple Linear Regression
Starting up a Project

604 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