1

我想代碼一些自我bot做我的自我託管的WordPress博客上的一些東西,所以我設法成功登錄使用webrequests有一些問題,但解決了他們。 但現在我試圖去永久鏈接頁面例如來獲取一些數據,但當我試圖使用登錄cookie做它時,它只是將我重定向到登錄頁面,就像我沒有使用登錄cookie。WordPress的HttpWebrequest vb.net

所以基本上這是登錄功能:

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 

它完美的作品,讓我到網址的主頁,並讓我發現,即時通訊登錄 但是當林推出此代碼去固定鏈接編輯。頁面只是返回登錄頁面源代碼意味着它無法登錄。

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() 

回答

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 
+0

Hi和感謝回答。我知道這個XMLRPC,但我認爲它只能發佈文章,它基本上不是我需要的atm。我正在尋找更改永久鏈接結構或刪除widjets和這樣的東西。我不認爲這太難,我只是有這些cookies的問題。 – user2979249

+0

在這種情況下,爲什麼不直接更新MySQL數據庫呢?直接更新永久鏈接將更容易,但無論如何檢查此鏈接:[https://stackoverflow.com/questions/4411397/asp-net-to-wordpress-sso-with-httpwebrequest)](https://stackoverflow。問題/ 4411397/asp-net-to-wordpress-sso-with-httpwebrequest) 來自鏈接: _響應返回system.net.cookie而不是system.web.httpcookie。我使用另一個cookie中的數據創建了一個新的httpcookie,它工作正常._ – MAlvarez

+0

不確定你是什麼意思,我們可以通過Skype或其他方式進行交談,以使其更快?謝謝! – user2979249

相關問題