VB.NET中的Post Request无法正常工作

时间:2016-06-17 12:24:05

标签: json vb.net post cookies httpwebrequest

我想用我保存在文件中的cookie执行此请求(Cookie很好,因为我可以执行其他页面请求)

http://www.banggood.com/index.php?com=event&t=recordSignInShare&fb_id=197203087314503_251208398580638&code=

是的,最后一个参数应为空。 当我在浏览器中打开它时,它会给我json数据(无论它们看起来如何) 但是当我尝试在vb.net应用程序上执行相同的请求时,它会将我重定向到另一个页面。 这是来自VB的代码:

 Dim postData As String = "com=event&t=recordSignInShare&fb_id=197203087314503_251208398580638&code="
    Dim bytes() As Byte = ASCIIEncoding.UTF8.GetBytes(postData)
    Dim postReq As HttpWebRequest = WebRequest.Create("http://www.banggood.com/index.php")
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = cookies
    'postReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
    postReq.Referer = "http://www.banggood.com/"
    postReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
    postReq.ContentLength = bytes.Length
    Dim postStream As Stream = postReq.GetRequestStream()
    postStream.Write(bytes, 0, bytes.Length)
    postStream.Close()
    Dim postResponse As HttpWebResponse
    postResponse = postReq.GetResponse()
    cookies.Add(postResponse.Cookies)
    Dim reader As New StreamReader(postResponse.GetResponseStream())
    Dim strSource As String = reader.ReadToEnd
    Return strSource

它返回html代码,而不是json数据:(

当我通过浏览器打开它时,这就是网络监视器的外观请求 请求:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:www.banggood.com
Referer:http://www.banggood.com/2016midyear.html?utmid=796
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
X-Requested-With:XMLHttpRequest

响应:     Cache-Control:max-age = 0,no-cache,no-store     连接:保持活跃     内容长度:94     内容类型:text / html的;字符集= utf-8的 我认为原始(来自代码)请求/响应标头并不重要,因为我可以通过在浏览器中打开此链接来正确执行它。

1 个答案:

答案 0 :(得分:0)

问题是,我需要得到请求:P 谢谢the_lotus