1

VB.NET でプログラムを書いていますが、httpwebrequest コードに問題があります。Firefox のライブ HTTP ヘッダーを使用して正しい投稿データを取得しましたが、何らかの理由でログインできません。

問題は、別の同様のサイトに同じコードを使用して (投稿データのみを変更して)、問題なく動作することです。

何かご意見は?これが私のコードです...

Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.ComponentModel
Imports System.Threading

Public Class Form1
        Dim logincookie As CookieContainer
        Dim html As String = "BGW still running"

        Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click

            Dim username As String = "username"
            Dim password As String = "password"

            Dim postData As String = "url=&username=" + username + "&password=" + password + "&login=Check+Mail%21"
            Dim tempcookies As New CookieContainer
            Dim encoding As New UTF8Encoding
            Dim bytedata As Byte() = encoding.GetBytes(postData)

            Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.website.com/inbox.aspx?login"), HttpWebRequest)
            postReq.Method = "POST"
            postReq.KeepAlive = True
            postReq.CookieContainer = tempcookies
            postReq.ContentType = "application/x-ww-form-urlencoded"
            postReq.Referer = "http://www.website.com/inbox.aspx?login"
            postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0"
            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 matchCount As Integer = 0

        Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.website.com/editprofile.aspx")
            request.CookieContainer = logincookie
            Dim response As System.Net.HttpWebResponse = request.GetResponse
            Dim reader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())


            html = reader.ReadToEnd
    End Sub

End Class

ご意見やご提案をお待ちしております。

4

2 に答える 2

1

表示される URL ではログインが異なります。コード内の URL とは異なる URL へのリダイレクトです。

于 2012-06-03T05:49:42.487 に答える