2013-10-29 109 views
0

我正嘗試使用webrequest和POST登錄到網站。需要登錄的網站的Webrequest

我使用Chrome的檢查元素,以便查看正在發佈什麼數據:
在上登錄後請求頭包含以下信息:

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 

,但它不是在洛

回答

1

基本上你在談論一個屏幕抓取,首先使用web請求獲取包含的頁面。隱藏的召喚字段,那麼你可以使用類似HtmlAgilityPack的東西來解析html,並獲得包含在你的文章中的值。

還記得在使用cookie的經過驗證的網站上工作時,您的HttpWebRequest需要附加到每個請求的cookie容器上,以確保驗證請求返回的cookie隨每個後續請求一起流動。

+0

謝謝,我會試試看,並回來。 –