来自需要登录的网站的Webrequest

时间:2013-10-29 15:22:48

标签: .net vb.net httpwebrequest webrequest httpwebresponse

我正在尝试使用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 字段是来自密码&amp;的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

但它不是登录。

1 个答案:

答案 0 :(得分:1)

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

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

相关问题