【问题标题】:Post Request in VB.NET doesn't work properlyVB.NET 中的发布请求无法正常工作
【发布时间】:2022-02-01 12:05:23
【问题描述】:

我想用我保存在文件中的 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

回应: 缓存控制:max-age=0,无缓存,无存储 连接:保持活动 内容长度:94 内容类型:文本/html;字符集=utf-8 我认为原始(来自代码)请求/响应标头并不重要,因为我只需在浏览器中打开此链接即可正确执行它。

【问题讨论】:

  • 当您在浏览器中复制/粘贴该网址时,它会执行 GET 而不是 POST。
  • 我知道,但收到的其他请求,我使用了 post 并且它们起作用了。我会检查你的想法
  • 当您尝试时,请确保您将信息作为查询字符串(网址的一部分)发送。

标签: json vb.net post cookies httpwebrequest


【解决方案1】:

问题是,我需要获取请求:P 谢谢the_lotus

【讨论】:

    【解决方案2】:
                    Dim urlphp As String = "" & dominio & "" & carpetanoti & "/demanda_alta.php"
                    Dim Conexion As HttpWebRequest = CType(WebRequest.Create(urlphp), HttpWebRequest)
                    Conexion.Method = "POST"
                    Conexion.ContentType = "application/x-www-form-urlencoded"
                    Dim POST_DATA As String = ("&cTitulo=" & "Te necesitamos!!" & "&cMensaje=" & "ALTA DEMANDA TENEMOS DOMICILIOS PARA TI" & "")
                    Dim byteArray() As Byte = Encoding.UTF8.GetBytes(POST_DATA)
                    Conexion.ContentLength = byteArray.Length
                    Dim FLUJO As Stream = Conexion.GetRequestStream()
                    FLUJO.Write(byteArray, 0, byteArray.Length)
                    FLUJO.Close()
                    Dim Response As HttpWebResponse = Conexion.GetResponse()
                    FLUJO = Response.GetResponseStream()
                    Dim LEER As New StreamReader(FLUJO)
                    Dim ServerResponse As String = LEER.ReadToEnd()
                    LEER.Close()
                    FLUJO.Close()
                    Response.Close()
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2014-04-21
    • 2021-04-17
    相关资源
    最近更新 更多