We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

httpwebrequest not getting all cookies

Medium Priority
1,564 Views
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:1.9.0.11) 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

Comment
Watch Question

Top Expert 2009

Commented:
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 --
Top Expert 2009

Commented:
> 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

Commented:
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)
Top Expert 2009

Commented:
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 ;-)
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.