Reputation: 1
I've recently tried to log into source.amazon.com via http web request post method. I've got everything right but everytime I try to create a request. I get "Cookies not enabled". I'm using a cookie container but still. Here's my code: [Any help would be greatly appreciated]
Dim tempCookies As CookieContainer
Dim logincookie As CookieContainer
Dim postData As String = "appActionToken=bPQVeahS2jIO4B1Cb7M53hzMOFEj3D&appAction=SIGNIN&openid.pape.max_auth_age=ape%3AMA%3D%3D&openid.ns=ape%3AaHR0cDovL3NwZWNzLm9wZW5pZC5uZXQvYXV0aC8yLjA%3D&prevRID=ape%3AMTNKSlBHREM3WEJLOFZYRVI2QTA%3D&pageId=ape%3Aa29yX2IyYnBvcnRhbF9uYQ%3D%3D&openid.identity=ape%3AaHR0cDovL3NwZWNzLm9wZW5pZC5uZXQvYXV0aC8yLjAvaWRlbnRpZmllcl9zZWxlY3Q%3D&openid.claimed_id=ape%3AaHR0cDovL3NwZWNzLm9wZW5pZC5uZXQvYXV0aC8yLjAvaWRlbnRpZmllcl9zZWxlY3Q%3D&openid.mode=ape%3AY2hlY2tpZF9zZXR1cA%3D%3D&openid.assoc_handle=ape%3Aa29yX2IyYnBvcnRhbF9uYQ%3D%3D&marketPlaceId=ape%3AQTFWMlNZTFRNUFAzNjI%3D&openid.return_to=ape%3AaHR0cHM6Ly9zb3VyY2UuYW1hem9uLmNvbS8jL2Rhc2hib2FyZA%3D%3D&email=EMAIL&create=0&password=PASSWORD&metadata1=OAL6NohRZ7zCHzgzUIxnrUVUf8OJ%2F%2BSqjeyfxKqF7GP%2B8P4eei5ozS7eVbWpYaxOJ0pZmsgS7uNT7yWrk0eN5XHYn%2FsM6nm4dgGOaXwTA7sOwm8YMUizaxcmZakGUhGnHk%2BJ5hyEIVMDk0CZTP8IZrstTyPlpkQERj5c3b4y2JrbWgLVu0PaYTF1xunw1aSWobbEw9aaO3%2FSXUL6KmTkHYTaJ%2BX3rnqdkykYeVX7%2FSD%2FWq1sRaJ6y3eymefpoKRfbeYNqmpPbuVrgDKickIfy%2BTfUpeVm2H0uuUawHf6lhuhX4NJbsQZBMR9hPN631Ulufr%2FRWfMTS8SXY%2BI9F8H9HT%2F8gsYSdSR8W3umRmGjDZeD1melAJLSbVoIx3%2B2tSTSqlJ6wG0%2F9Pi7RIL5BQHQuEPg%2FRoGjWbZj60kfOirI59pn91r%2FtNFJKP2EXE7G14rcT57zu25UEg2QEE20Br668jhjOwR0I47CERdXFDdINPxH5rfDIbRM4Axm1fdfSF08S1DeTI9Yq4H%2BNWvtn%2Bsu%2FfL5un3b7JWNOKcZHRRbj8%2FKhg2LssGOwc9S4yiDiiGuFdkxlMawc9Pn3ewddTQkPtOz6PgMQENr6udpvNLaEyMKA7T2KWE2SacabX2OiJbkh04lp5wuoM4n86G9eP0yHAk1TZxKQc%2B2vYyoUJHxojLXqKREi5O%2Fv5lBpeN%2Bk6jQMZjF7MbQbxefAWlQ%2BljSbZ2bug7axwPdTAlBs239z8zMpOBG0y4%2F47iyMViymqYPKPDoS3%2BZN47baYUVDdp1mUbS71LO74YKaEJEjdfNeT%2B5faXblFCI2e39PSZa%2FlnvNvQU%2Bmkz%2BQDmn2kk62X5tio15DsGp%2FS3Nm3z3XImRlqLsxgqXSzApKPtK6OtHLqGIc5WWpJOXGPqcIMQ2aUuJaMmHfulzPNM06T6Tbp470xznqGwzwHYtEAN3%2FnnvCqsKs2i05yFKpsn1rpXEG5MjtMHth3uXBfItT61xuweapDYR4rqGBCAjNWooUe4Nlvpg2B42scDubRunuYzu7FlcmprrBxhqK1C9xJFC4gHVktX3NaKvTHMWa3gFlWq%2FEsP6ZBgNixtC3VmJKm7OvdzRxelw%3D"
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://source.amazon.com/ap/signin?ie=UTF8&openid.pape.max_auth_age=0&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.assoc_handle=kor_b2bportal_na&marketPlaceId=A1V2SYLTMPP362&openid.return_to=https%3A%2F%2Fsource.amazon.com%2F%23%2Fdashboard"), HttpWebRequest)
Dim tempCookies As New CookieContainer
postReq.Method = "POST"
postReq.CookieContainer = tempCookies
postReq.KeepAlive = True
postReq.ContentType = "application/xhtml+xml"
postReq.Referer = "https://source.amazon.com/ap/signin?ie=UTF8&openid.pape.max_auth_age=0&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.assoc_handle=kor_b2bportal_na&marketPlaceId=A1V2SYLTMPP362&openid.return_to=https%3A%2F%2Fsource.amazon.com%2F%23%2Fdashboard"
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
Upvotes: 0
Views: 926
Reputation: 133
I've been working on nearly the exact same thing.. and I've made some progress. add this to the top of your code. (basically what this is doing is capturing a cookie you receive when you first visit the page, your code will later reuse it without you having to make any further changes)
a few things...
move your cookie container declaration to the very top.
Where I have "http://www.amazon.com" may need to be changed in both lines.
Change the UserAgent to what you need.
THIS STILL DOESN'T WORK... because.... Amazon's referrer links and appActionToken link are constantly changing.
once we overcome this problem, we should be able to get this to work, if its possible to somehow catch these links in real time. I'm thinking a webrequest might help, then again the links could change as soon as they are issued.
I got this working for another site that gives a non http:// link like where the appActionToken is, however their links are static and do not change. I was able to use their domain (ie: http:\www.website.com) where I have http:\www.amazon.com placed. Though, this link might need to be changed to their log-in page.
If you make any further progress, I would love to hear about it!
Dim tempcookie As New CookieContainer
Dim postreq1 As HttpWebRequest = DirectCast(WebRequest.Create("http://www.amazon.com"), HttpWebRequest)
Dim postresponse1 As HttpWebResponse
postresponse1 = DirectCast(postreq1.GetResponse(), HttpWebResponse)
postreq1 = DirectCast(HttpWebRequest.Create("http://www.amazon.com"), HttpWebRequest)
postreq1.UserAgent = "mozilla/5.0 (windows nt 5.1) applewebkit/537.36 (khtml, like gecko) chrome/40.0.2214.111 safari/537.36"
postreq1.Timeout = 60000
postreq1.CookieContainer = tempcookie
postreq1.Method = "get"
postreq1.AllowAutoRedirect = True
postreq1.ContentType = "application/x-www-form-urlencoded"
postresponse1 = DirectCast(postreq1.GetResponse(), HttpWebResponse)
Dim datastream As Stream = postresponse1.GetResponseStream()
Dim reader As New StreamReader(datastream)
Dim responsefromserver As String = reader.ReadToEnd()
also the bottom 3 lines may or may not be required.
Here is the rest of my code just in case.
Dim postdata As String = "appActionTokenXXXXXXXXXXXXXXX THIS CHANGES"
'Dim tempcookie As New CookieContainer
Dim encoding As New UTF8Encoding
Dim bytesdata As Byte() = encoding.GetBytes(postdata)
Dim postreq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.amazon.com/XXXXX - THESE CHANGE"), HttpWebRequest)
postreq.Method = "POST"
postreq.KeepAlive = True
postreq.CookieContainer = tempcookie
postreq.ContentType = "application/x-www-form-urlencoded"
postreq.Referer = "https://www.amazon.com/XXXXX - THESE CHANGE"
postreq.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
postreq.ContentLength = bytesdata.Length
Dim postreqstream As Stream = postreq.GetRequestStream()
postreqstream.Write(bytesdata, 0, bytesdata.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postreq.GetResponse(), HttpWebResponse)
tempcookie.Add(postresponse.Cookies)
logincookie = tempcookie
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
Imports System.Net
Imports System.IO
Imports System.Text
Upvotes: 1