VB.Net httpwebrequest登录和下载页面

时间:2017-10-09 07:05:53

标签: vb.net cookies login httpwebrequest webclient

我想用我的帐户登录VyprVPN网站。

所以我使用下面的代码成功登录,但我希望获得剩余数据的数量,这些数据存储在此网址:https://www.goldenfrog.com/controlpanel/vpn-remaining中作为纯HTML。

所以基本上我要做的就是在登录后从网址下载html。

我的登录代码:

Dim username As String = "myusername"
Dim password As String = "mypassword"

Dim postData As String = "username=" & username & "&password=" & password & "&login=Login"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)

Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.goldenfrog.com/controlpanel/login"), HttpWebRequest) 'prva linija
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.Timeout = 15000
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://www.goldenfrog.com/login"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
postReq.ContentLength = byteData.Length

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

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

Dim thepage As String = postreqreader.ReadToEnd

我尝试了什么(没有工作,显示空白消息框,这是访问该网址而未登录的结果,我可能无法分配正确的cookie):

Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://www.goldenfrog.com/controlpanel/vpn-remaining"), HttpWebRequest)
request.CookieContainer = tempCookies
request.Method = "POST"
request.KeepAlive = True
request.CookieContainer = logincookie
request.ContentType = "application/x-www-form-urlencoded"
request.Referer = "https://www.goldenfrog.com/controlpanel"
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
request.ContentLength = byteData.Length

Dim postreqstream2 As Stream = request.GetRequestStream()
postreqstream2.Write(byteData, 0, byteData.Length)
postreqstream2.Close()
Dim postresponse2 As HttpWebResponse
postresponse2 = DirectCast(request.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse2.Cookies)
logincookie = tempCookies
Dim postreqreader2 As New StreamReader(postresponse.GetResponseStream())
Dim thepage2 As String = postreqreader.ReadToEnd

MsgBox(thepage2)

1 个答案:

答案 0 :(得分:0)

Dim request As HttpWebRequest = DirectCast(WebRequest.Create("url"), HttpWebRequest)
request.CookieContainer = logincookie
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
Dim theusercp As String = reader.ReadToEnd