WordPress HttpWebrequest vb.net

I am trying to code some self bot to do some stuff for me on my self hosted wordpress blogs, so I managed to login successfully using webrequests had some problems but solved them.
But now I’m trying to go to permalinks page for example to get some data but when Im trying to do it using the login cookie it just redirects me back to login page like I’m not using the login cookie.

So basically this is the login function:

Read More
 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

It works perfect, gets me to the URL homepage and shows me that Im logged in.
but when Im launching this code to get to the permalinks edit page it just returns the login page source code means it wasn’t able to login.

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

Related posts

Leave a Reply

1 comment

  1. Here is an alternative. Try the WordPress API (xmlrpc.php) to access and modify data in your WordPress installation. I have used CookComputing.XmlRpc to achieve this.

    Here is a sample on how to create a new WordPress post:

    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