HttpWebRequest add item to cart then "post" to change quantity problem
Hi all, I have problem on use HttpWebRequest with "POST" method.
First, I access
http://store.apple.com/hk-zh/configu...=UNLOCKED%2FWW
this link and go to the cart page.
Then when I change the quantity to 2, I had see the live http header to use "POST" and submit the content to the server.
I follow the header and write with vb2008, but I get the server return 503 error on the code that getting response, would you mind to tell me that I got problem on where?
Here is my code:
Code:
Dim logincookie As CookieContainer
Dim response As HttpWebResponse
Dim tempCookies As New Cookie
Dim req As HttpWebRequest
Dim postdata As String
Dim encoding As New UTF8Encoding
Dim byteData As Byte()
Dim quant1 As String
Dim quant2 As String
Dim quantint As Integer
Dim totalquan As Integer
Dim cartid As String
Dim quant3 As String
Dim htmltext As String
Dim cartid2 As String
Dim reqreader As StreamReader
Dim postreqstream As Stream
logincookie = New CookieContainer()
Try
'access item page
req = DirectCast(WebRequest.Create("http://store.apple.com/hk-zh/configure/MD198ZP/A?option.iphone_applecare=none&option.iphone_bumpers=none&option.iphone_4_dock=none&option.additional_apple_usb_power_adaptor=none&option.additional_apple_dock_connector_to_usb_cable=none&add-to-cart=add-to-cart&cppart=UNLOCKED%2FWW"), HttpWebRequest)
req.Method = "GET"
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
req.Headers("Accept-Language") = "zh-tw,en-us;q=0.7,en;q=0.3"
'req.Headers("Accept-Encoding") = "gzip, deflate"
req.Headers("Accept-Charset") = "UTF-8,*"
req.KeepAlive = True
req.CookieContainer = logincookie
response = DirectCast(req.GetResponse(), HttpWebResponse)
For Each tempCookies In response.Cookies
logincookie.Add(tempCookies)
Next
reqreader = New StreamReader(response.GetResponseStream())
htmltext = reqreader.ReadToEnd
reqreader.Close()
response.Close()
'use "POST" to change the quantity to 2
req = DirectCast(WebRequest.Create("http://store.apple.com/hk-zh/hybrid_cartx"), HttpWebRequest)
req.Method = "POST"
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
req.Headers("Accept-Language") = "zh-tw,en-us;q=0.7,en;q=0.3"
'req.Headers("Accept-Encoding") = "gzip"
req.Headers("Accept-Charset") = "UTF-8,*"
req.KeepAlive = True
req.Headers("X-Requested-With") = "XMLHttpRequest"
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
req.Referer = "http://store.apple.com/hk-zh/cart"
req.CookieContainer = logincookie
req.Headers("Pragma") = "no-cache"
req.Headers("Cache-Control") = "no-cache"
'construct POST data and write to stream
quant1 = "maxlength=\""3\"" id=\"""
quant2 = "quantity\"" /> <\/li> <li class=\"
quantint = InStr(htmltext, quant1) + quant1.Length - 1
cartid = htmltext.Substring(quantint, InStr(htmltext, quant2) - 1 - quantint)
totalquan = CInt(TextBox1.Text)
postdata = cartid & "quantity=" & totalquan.ToString
postdata += "&" & cartid & "packageText=&" & cartid & "messageText=&"
quant3 = "_a=up&cartid="
cartid2 = htmltext.Substring(InStr(htmltext, quant3) - 1, 100)
postdata += cartid2.Substring(0, InStr(cartid2, Chr(34)) - 1)
byteData = encoding.GetBytes(postdata)
req.ContentLength = byteData.Length
postreqstream = req.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
'server return 503 in this code
response = DirectCast(req.GetResponse(), HttpWebResponse)
reqreader = New StreamReader(response.GetResponseStream())
htmltext = reqreader.ReadToEnd
reqreader.Close()
response.Close()
WebBrowser1.DocumentText = htmltext
Catch ex As Exception
MsgBox(ex.ToString & vbCrLf & ex.Message.ToString)
End Try
I had comment req.Headers("Accept-Encoding") = "gzip, deflate" because when I set the encoding to server, the response are encoded and I don't know how to decode...
Thanks a lot:(
Re: HttpWebRequest add item to cart then "post" to change quantity problem
oh...there is a code button...
sorry for I haven't use the code button to display code...
Re: HttpWebRequest add item to cart then "post" to change quantity problem
You can click edit and wrap the code inside tags.
Re: HttpWebRequest add item to cart then "post" to change quantity problem
Quote:
Originally Posted by
ident
You can click edit and wrap the code inside tags.
Thanks.
Now I can edit post:D
Re: HttpWebRequest add item to cart then "post" to change quantity problem
Would anyone can help me:(