2010-08-20 103 views
1

嘿,已經結束了。我一直在看一些示例源代碼,但我無法弄清楚。我想通過我的帳戶發送POST請求以登錄到網站。例如,如何使用HttpWebRequest登錄此網站。VB.NET HttpWebRequest POST

http://z4.invisionfree.com/cotec/index.php

這是我爲我的部落建立的應用程序,您必須在論壇上有一個帳戶才能打開該應用程序,因此如果登錄工作,它會打開。

回答

1

爲什麼不利用這段代碼來簡化你的努力:http://joel.net/code/easyhttp.aspx

+0

那麼我將如何使用類發送登錄請求? EasyHttp.Send(「http://z4.invisionfree.com/cotec/index.php?」,「username = USER&password = PASS」) 就是這樣嗎? – peppersock 2010-08-20 18:08:13

+0

你可以。只要確保無論表單字段的名稱是什麼,都可以按照您展示的方式指定它們。 – 2010-08-20 19:33:56

2
dim email as string = "your email" 
dim pass as string = "your pass" 

    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://touch.facebook.com"), HttpWebRequest) 
       postReq.Method = "GET" 
       postReq.KeepAlive = True 
       postReq.CookieContainer = logincookie 
       postReq.ContentType = "application/x-www-form-urlencoded" 
       postReq.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3" 

       Dim postresponse As HttpWebResponse = DirectCast(postReq.GetResponse(), HttpWebResponse) 
       logincookie.Add(postresponse.Cookies) 

       Dim postreqreader As New StreamReader(postresponse.GetResponseStream()) 

       Dim infos As String = postreqreader.ReadToEnd 

       '--------------------------- 

       Dim byteData As Byte() = encoding.GetBytes("lsd=&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=1&ajax=0&width=0&pxr=0&gps=0&m_ts=&li=&email=" & email.Replace("@", "%40") & "&pass=" & pass & "&login=Connexion") 

       postReq = DirectCast(WebRequest.Create("https://touch.facebook.com/login.php?refsrc=https%3A%2F%2Ftouch.facebook.com%2F&refid=8"), HttpWebRequest) 
       postReq.Method = "POST" 
       postReq.KeepAlive = True 
       postReq.CookieContainer = logincookie 
       postReq.ContentType = "application/x-www-form-urlencoded" 
       postReq.Referer = "https://touch.facebook.com/" 
       postReq.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3" 
       postReq.ContentLength = byteData.Length 

       Dim postreqstream As Stream = postReq.GetRequestStream() 
       postreqstream.Write(byteData, 0, byteData.Length) 
       postreqstream.Close() 

       postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse) 
       logincookie.Add(postresponse.Cookies) 

       postreqreader = New StreamReader(postresponse.GetResponseStream())