0

これは私を狂わせています。私はこれに近づかなければならないことを知っています。私のリクエストは、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
4

1 に答える 1

1

仕事上の制限のため、私は帝国でそれを試すことができませんでしたが、これを試してみてください:

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

それがあなたのために働くことを願っています

于 2013-02-28T06:30:24.980 に答える