vb.net httpwebrequest to login to EmpireAvenue.com



我快疯了。我知道我必须密切关注这件事。据我所知,我的请求和真实的请求是一致的,只是cookie有点不同。我似乎错过了谷歌的分析。不确定这是不是问题所在。我得到重定向像我应该,但在重定向页面它要求我再次登录。任何帮助都是感激的。下面是我的代码:

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

由于工作上的限制,我不能在经验收入上尝试,但试试这个:

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

希望它对你有用

最新更新