1

我正在尝试编写一些自我机器人来在我的自托管 wordpress 博客上为我做一些事情,所以我设法使用 webrequests 成功登录,但遇到了一些问题,但解决了。但是现在我正在尝试转到永久链接页面以获取一些数据,但是当我尝试使用登录 cookie 执行此操作时,它只是将我重定向回登录页面,就像我没有使用登录 cookie 一样。

所以基本上这是登录功能:

 cleanurl = TextBox3.Text
    logincookie = New CookieContainer

    Dim url As String = cleanurl & "/wp-login.php"
    Dim postreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    Dim cookies As New CookieContainer

    postreq.CookieContainer = cookies
    postreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b6pre) Gecko/20100903 Firefox/4.0b6pre"
    postreq.KeepAlive = True
    postreq.Timeout = 120000
    postreq.Method = "POST"
    postreq.Referer = url

    postreq.AllowAutoRedirect = False
    postreq.ContentType = "application/x-www-form-urlencoded"

    Dim postData As String = "log=" & TextBox1.Text & "&pwd=" & TextBox2.Text & "&wp-submit=Log+In&redirect_to=" & cleanurl & "/wp-admin" & "&testcookie=1"
    Dim encoding As New UTF8Encoding
    Dim byteData As Byte() = encoding.GetBytes(postData)
    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)


    '/ The Following is set next request with authentication cookie

    Dim nextreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(cleanurl), HttpWebRequest)
    nextreq.ContentType = "application/x-www-form-urlencoded"
    nextreq.Method = "GET"
    nextreq.CookieContainer = New CookieContainer

    nextreq.CookieContainer.Add(postresponse.Cookies)


    nextreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b6pre) Gecko/20100903 Firefox/4.0b6pre"
    nextreq.KeepAlive = True

    Dim nextresponse As HttpWebResponse
    nextresponse = DirectCast(nextreq.GetResponse, HttpWebResponse)

    logincookie = nextreq.CookieContainer
    logincookie.Add(nextresponse.Cookies)



    Dim postreqreader As New StreamReader(nextresponse.GetResponseStream())
    Dim thepage As String = postreqreader.ReadToEnd

    nextresponse.Close()

    RichTextBox1.Text = thepage
    WebBrowser1.DocumentText = thepage
    Refresh()

    If thepage.Contains("ERROR") Then
        MsgBox("Error logging in!")
    Else
        MsgBox("Lets Start Blogging!")

    End If

它运行完美,让我进入 URL 主页并向我显示我已登录。但是当我启动此代码以进入永久链接编辑页面时,它只返回登录页面源代码意味着它无法登录。

Dim nextreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(cleanurl & "/wp-admin/options-permalink.php"), HttpWebRequest)
    nextreq.ContentType = "application/x-www-form-urlencoded"
    nextreq.Method = "GET"
    nextreq.CookieContainer = logincookie



    nextreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b6pre) Gecko/20100903 Firefox/4.0b6pre"
    nextreq.KeepAlive = True


    Dim nextresponse As HttpWebResponse = DirectCast(nextreq.GetResponse, HttpWebResponse)

    nextreq.CookieContainer.Add(nextresponse.Cookies)



    Dim postreqreader As New StreamReader(nextresponse.GetResponseStream())
    Dim thepage As String = postreqreader.ReadToEnd

    nextresponse.Close()

    RichTextBox1.Text = thepage
    WebBrowser1.DocumentText = thepage
    Refresh()
4

1 回答 1

0

这是一个替代方案。尝试使用 Wordpress API (xmlrpc.php) 来访问和修改 Wordpress 安装中的数据。我已经使用 CookComputing.XmlRpc 来实现这一点。

以下是有关如何创建新 WordPress 帖子的示例:

Imports CookComputing.XmlRpc

Public Sub Add_New_Post_Sample()
    Dim NewPostContent As New NewPostContent
    NewPostContent.post_type = "post"
    NewPostContent.post_status = "draft"
    NewPostContent.post_title = "MyTitle"
    NewPostContent.post_author = "1"
    NewPostContent.post_excerpt = ""
    NewPostContent.post_content = "MyContent"
    NewPostContent.post_date_gmt = "2015-01-21 00:00:00"
    NewPostContent.post_name = "my-unique-url"
    NewPostContent.post_password = ""
    NewPostContent.comment_status = "closed"
    NewPostContent.ping_status = "closed"
    NewPostContent.sticky = False
    NewPostContent.post_thumbnail = 0
    NewPostContent.post_parent = 0
    NewPostContent.Mycustom_fields = New String() {""}

    Dim API = DirectCast(XmlRpcProxyGen.Create(GetType(IgetCatList)), IgetCatList)

    Dim clientprotocal = DirectCast(API, XmlRpcClientProtocol)
    clientprotocal.Url = "http://myurl.com/xmlrpc.php"

    Dim result As String = API.NewPost(1, "admin", "password", NewPostContent)

    Console.WriteLine(result)
End Sub

Public Structure NewPostContent
    Public post_type As String
    Public post_status As String
    Public post_title As String
    Public post_author As Integer
    Public post_excerpt As String
    Public post_content As String
    Public post_date_gmt As DateTime
    Public post_name As String
    Public post_password As String
    Public comment_status As String
    Public ping_status As String
    Public sticky As Boolean
    Public post_thumbnail As Integer
    Public post_parent As Integer
    Public Mycustom_fields As String()
End Structure
于 2015-04-29T15:30:41.927 回答