0

我正在尝试使用 webrequest 和 POST 登录网站。

我使用 Chrome 的 Inspect Element 来查看正在发布哪些数据:
在登录 POST 时,请求标头包含以下信息:

ret=%2Fro%2Findex.php&sha1=2254****79a19&summon=8b5df8dc0c10323669f43105848ad40b8953fc8e&username=gabriel.zanc&password=&login=Conectare

我无法使用 webrequest 登录,因为“召唤”值从网页中硬编码为 html,如下所示:

<input type=hidden name=summon value='4974d2b410da0ed9abf7079f461eac22d02fb3c9'>

并在每次会话中更改。

sha1字段是密码传票的SHA1 编码。

任何帮助或指出正确的方向将不胜感激。

谢谢。

后来:
我试过这个:

 Dim username As String = "accName"
    Dim password As String = "pass"
    Dim summon As String = ""

    ' Connect to WebSite
    Dim wbReq As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create("http://www2.gpstracking.ro/ro/login.php?ret=%2Fro%2Findex.php"), Net.HttpWebRequest)
    Dim wbResp As Net.HttpWebResponse = DirectCast(wbReq.GetResponse(), Net.HttpWebResponse)
    Dim wbHCol As Net.WebHeaderCollection = wbResp.Headers
    Dim wbCookieJar As New CookieContainer
    wbReq.CookieContainer = wbCookieJar
    Dim myStream As IO.Stream = wbResp.GetResponseStream()
    Dim myreader As New IO.StreamReader(myStream)
    Me.TextBox2.Text = myreader.ReadToEnd
    Dim doc As New HtmlAgilityPack.HtmlDocument
    doc.LoadHtml(Me.TextBox2.Text)
    ' get summon value
    For Each input As HtmlNode In doc.DocumentNode.SelectNodes("//input")
        If input.Attributes("name").Value = "summon" Then
            summon = input.Attributes("value").Value
        End If
    Next
    'login
    Dim sha1Obj As New System.Security.Cryptography.SHA1CryptoServiceProvider
    Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(password + summon)
    bytesToHash = sha1Obj.ComputeHash(bytesToHash)
    Dim sha1 As String = ""
    For Each b As Byte In bytesToHash
        sha1 += b.ToString("x2")
    Next
    Dim postdata = System.Text.Encoding.Default.GetBytes("ret=%2Fro%2Findex.php&sha1=" + sha1 + "&summon=" + summon + "&username=" + username + "&password=&login=Conectare")

    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www2.gpstracking.ro/ro/login.php"), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = wbCookieJar
    postReq.ContentType = "application/x-www-form-urlencoded"
    postReq.Referer = "http://www2.gpstracking.ro/ro/login.php?ret=%2Fro%2Findex.php"
    postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0"
    postReq.ContentLength = postdata.Length

    Dim postreqstream As Stream = postReq.GetRequestStream()
    postreqstream.Write(postdata, 0, postdata.Length)
    postreqstream.Close()
    Dim postresponse As HttpWebResponse

    postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
    wbCookieJar.Add(postresponse.Cookies)
    Dim logincookie = wbCookieJar
    Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

    Me.TextBox1.Text += postreqreader.ReadToEnd

但它没有登录。

4

1 回答 1

1

基本上你在谈论屏幕抓取,首先使用网络请求来获取包含隐藏召唤字段的页面,然后你可以使用HtmlAgilityPack之类的东西来解析 html 并获取要包含在你的帖子中的值。

还请记住,在使用使用 cookie 的经过身份验证的网站时,您的 HttpWebRequest 需要为每个请求附加到 cookie 容器,以确保身份验证请求返回的 cookie 与每个后续请求一起流动。

于 2013-10-29T16:01:12.023 回答