如何捕获发送到重定向URL的帖子数据(VB 2010)

时间:2013-11-23 09:54:33

标签: vb.net

我正在尝试通过我的Windows窗体应用程序(VB 2010)登录到一个网页并获得一些响应,但我不知道该怎么做。

此处描述了服务器服务: https://api.developer.betfair.com/services/webapps/docs/display/1smk3cen4v3lu3yomq5qye0ni/Interactive+Login+from+a+Desktop+Application

一开始我尝试将WebBrowser Control插入到我的表单中,但我在这里阅读了许多文章,使用BeforeNavigate2事件是一种旧方法。

我想这样做:

  1. 向网络服务器发送HTTP请求,包括用户名和密码
  2. 抓住发送到重定向网址的发布数据
  3. 阅读POST请求正文get getStatus和productToken(SSOID)
  4. 这是我的代码:

    Private Sub getPOST()
        ' Create a request for the URL
        Dim request As WebRequest = _
            WebRequest.Create("https://identitysso.betfair.com/view/login?product=82&url=https://www.betfair.com&username=abc&password=abc")
        request.Method = "POST"
        ' Get the response
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        RichTextBox1.Text = responseFromServer
        ' Cleanup the streams and the response.
        reader.Close()
        dataStream.Close()
        response.Close()
    End Sub
    

    但是这段代码返回的页面HTML代码不是loginStatus和SSOID。

1 个答案:

答案 0 :(得分:0)

我检查过这个页面,发现你的代码存在一些问题: 首先,您发布的网址错误,因为www.betfair.com/view/login表单有action =“/ api / login”,而不是“/ view / login”。其次,您在QueryString而不是POSTDATA中传递您的用户名/密码...此表单使用method = post。第三,您没有通过所有必填字段。此代码段应解决这些描述的问题,并回答有关获取重定向网址的问题:

Sub TestWebClient()

    ' Create a request for the URL
    ' You were using the wrong URL & passing the values improperly
    ' I also changed this to HttpWebRequest, used to be WebRequest.
    Dim request As HttpWebRequest = WebRequest.Create("https://identitysso.betfair.com/api/login") '
    request.Method = "POST"

    ' New code added
    request.ContentType = "application/x-www-form-urlencoded"

    ' New code added, this prevents the following of the 302 redirect in the Location header
    request.AllowAutoRedirect = False

    ' New code added, this sends the values as POSTDATA, which is contained inside the HTTP POST request rather than in the URL
    Using writer As New StreamWriter(request.GetRequestStream)
        writer.Write("username=abc")
        writer.Write("&password=abc")
        writer.Write("&login=true")
        writer.Write("&redirectMethod=POST")
        writer.Write("&product=82")
        writer.Write("&url=https://www.betfair.com/")
        writer.Write("&ioBlackBox=")
    End Using

    ' Get the response
    ' This will print the Location header (aka "Redirect URL") on the screen for you
    ' Make decisions as needed.
    Dim response As HttpWebResponse = request.GetResponse()
    Console.WriteLine("HTTP RESPONSE HEADERS:")
    For Each item In response.Headers
        Console.WriteLine(item & "=" & response.Headers(item))
    Next
    Console.ReadLine()

End Sub