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()
Hi和感謝回答。我知道這個XMLRPC,但我認爲它只能發佈文章,它基本上不是我需要的atm。我正在尋找更改永久鏈接結構或刪除widjets和這樣的東西。我不認爲這太難,我只是有這些cookies的問題。 – user2979249
在這種情況下,爲什麼不直接更新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
不確定你是什麼意思,我們可以通過Skype或其他方式進行交談,以使其更快?謝謝! – user2979249