vb.net httpwebrequest to login to EmpireAvenue.com
这让我发疯。 我知道我必须对此保持警惕。 据我所知,我的请求与真实请求匹配,只是cookie有所不同。 我似乎想念Google Analytics(分析)的那些。 不知道这是否是问题。 我得到了应有的重定向,但是在重定向页面上,它要求我再次登录。 任何帮助表示赞赏。 这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | Private Function eaLogin(ByVal ticker As String, ByVal password As String) Try ServicePointManager.Expect100Continue = False Dim request As HttpWebRequest = httpWebRequest.Create("http://www.empireavenue.com") request.Credentials = CredentialCache.DefaultCredentials request.CookieContainer = cookieJar Dim response As HttpWebResponse = request.GetResponse() Dim dataStream As Stream = response.GetResponseStream() Dim reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() reader.Close() response.Close() Dim session As String ="" ServicePointManager.Expect100Continue = False 'Set the initial parameters Dim UserID As String = ticker ' Username Dim PWord As String = HttpUtility.UrlEncode(password) ' Password Dim domain As String ="https://www.empireavenue.com/user/login/do" Dim encoding As New System.Text.ASCIIEncoding ' Use the appropriate HTML field names to stuff into the post header Dim PostData As String = _ "login_username=" & ticker & _ "&login_password=" & PWord Dim Data() As Byte = encoding.GetBytes(PostData) ' Initialise the request Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create(domain) ' Login location taken from the form action With LoginReq .KeepAlive = True .Method ="POST" ' Note: if the page uses a redirect if will fail .AllowAutoRedirect = False .ContentType ="application/x-www-form-urlencoded" .ContentLength = Data.Length ' Set empty container .CookieContainer = cookieJar .Referer ="http://www.empireavenue.com/" .UserAgent = userAgent .Accept ="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" .Host ="www.empireavenue.com" End With ' Add the POST data Dim SendReq As IO.Stream = LoginReq.GetRequestStream LoginReq.Headers.Add("Accept-Language","en-US,en;q=0.5") LoginReq.Headers.Add("Accept-Encoding","gzip, deflate") SendReq.Write(Data, 0, Data.Length) SendReq.Close() ' Obtain the response Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse() ' Retreive the headers from the request (e.g. the location header) Dim Redirect As String = LoginRes.Headers("Location") ' Add any returned cookies to the cookie collection cookieJar.Add(LoginRes.Cookies) ' Move to the redirected page as a GET request... Dim newUrl As String ="" If Not (Redirect Is Nothing) Then If Redirect.StartsWith("http://") Then newUrl = Redirect Else newUrl ="https://www.empireavenue.com" & Redirect End If LoginReq = Net.WebRequest.Create(newUrl) With LoginReq .KeepAlive = False .Method ="GET" .ContentType ="application/x-www-form-urlencoded" .AllowAutoRedirect = True .CookieContainer = cookieJar End With ' Perform the navigate and output the HTML LoginRes = LoginReq.GetResponse() Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream) Dim HTML As String = sReader.ReadToEnd If HTML.Contains(ticker) Then MessageBox.Show("yay!") Return True Else MessageBox.Show("no!") Return False End If Else MessageBox.Show("no too!") Return False End If Catch ex As Exception MessageBox.Show(ex.Message.ToString) Return False End Try End Function |
由于工作限制,我无法在empirevenue上尝试使用它,但是请尝试以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Dim tempCookies As CookieContainer ServicePointManager.Expect100Continue = False Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.empireavenue.com/user/login/do"), HttpWebRequest) Dim postData As String ="login_username=" & ticker &"&login_password=" & PWord Dim encoding As New UTF8Encoding Dim byteData As Byte() = encoding.GetBytes(postData) postReq.Method ="POST" postReq.KeepAlive = True postReq.CookieContainer = tempCookies postReq.ContentType ="application/x-www-form-urlencoded" postReq.Referer ="http://www.empireavenue.com/" postReq.UserAgent ="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120427 Firefox/15.0a1" 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 |
希望它对你有用