title: Wordpress HttpWebrequest vb.net
body: <p>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.</p>
  
  <p>So basically this is the login function:</p>
  
  <pre><code> cleanurl = TextBox3.Text
      logincookie = New CookieContainer
  
      Dim url As String = cleanurl &amp; "/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=" &amp; TextBox1.Text &amp; "&amp;pwd=" &amp; TextBox2.Text &amp; "&amp;wp-submit=Log+In&amp;redirect_to=" &amp; cleanurl &amp; "/wp-admin" &amp; "&amp;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
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>Dim nextreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(cleanurl &amp; "/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()
  </code></pre>