【问题标题】:HTTP Web Request works for other sites, but not this one. Any ideas? (VB.NET)HTTP Web 请求适用于其他站点,但不适用于这个站点。有任何想法吗? (VB.NET)
【发布时间】:2012-06-03 05:23:45
【问题描述】:

我正在用 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

感谢任何想法或建议。

【问题讨论】:

    标签: vb.net cookies httpwebrequest postdata


    【解决方案1】:

    您显示的 URL 的登录名不同。是否重定向到与您的代码中的 URL 不同的 URL。

    【讨论】:

    • 我只是插入了“网站”来代替实际的 URL。实际网址是 www.pof.com
    【解决方案2】:

    抱歉稍后回复,但这也会对其他人有所帮助..

    这是因为您缺少一个对于使用 HTTP WEBREQUEST 登录非常重要的 CookieContainer...

    这是一个惊人的教程!我使用相同的方法登录 MYBB FORUMS、vBULLETIN 和许多其他 JOOMLA、WORDPRESS 网站。并且一遍又一遍地使用同一个教程中的相同代码..

    代码如下:

    Imports System.Net
    Imports System.Text
    Imports System.IO
    
    Public Class Form1
    
    Dim logincookie As CookieContainer
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim postData As String = "referer=http%3A%2F%2Fforums.zybez.net%2Findex.php%3Fapp%3Dcore%26module%3Dglobal%26section%3Dlogin&username=" & TextBox1.Text & "&password=" & TextBox2.Text & "&rememberMe=1"
    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://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = tempCookies
    postReq.ContentType = "application/x-www-form-urlencoded"
    postReq.Referer = "http://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"
    postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
    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())
    
    End Sub
    
    End Class
    

    感谢“HowToStartProgramming.Com”

    【讨论】:

      猜你喜欢
      • 2022-08-21
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多