如何登录到WordPress编程? [英] How to login to wordpress programmatically?
问题描述
我需要以编程方式执行WordPress管理面板一些动作,但不能管理如何使用C#和HttpWebRequest的登录到WordPress的。
I need to perform some action in wordpress admin panel programmatically but can't manage how to login to Wordpress using C# and HttpWebRequest.
下面是我做的:
private void button1_Click(object sender, EventArgs e)
{
string url = "http://localhost/wordpress/wp-login.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
CookieContainer cookies = new CookieContainer();
SetupRequest(url, request, cookies);
//request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//request.Headers["Accept-Language"] = "uk,ru;q=0.8,en-us;q=0.5,en;q=0.3";
//request.Headers["Accept-Encoding"] = "gzip,deflate";
//request.Headers["Accept-Charset"] = "windows-1251,utf-8;q=0.7,*;q=0.7";
string user = "test";
string pwd = "test";
request.Credentials = new NetworkCredential(user, pwd);
string data = string.Format(
"log={0}&pwd={1}&wp-submit={2}&testcookie=1&redirect_to={3}",
user, pwd,
System.Web.HttpUtility.UrlEncode("Log In"),
System.Web.HttpUtility.UrlEncode("http://localhost/wordpress/wp-admin/"));
SetRequestData(request, data);
ShowResponse(request);
}
private static void SetupRequest(string url, HttpWebRequest request, CookieContainer cookies)
{
request.CookieContainer = cookies;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; uk; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)";
request.KeepAlive = true;
request.Timeout = 120000;
request.Method = "POST";
request.Referer = url;
request.ContentType = "application/x-www-form-urlencoded";
}
private void ShowResponse(HttpWebRequest request)
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
responseTextBox.Text = (((HttpWebResponse)response).StatusDescription);
responseTextBox.Text += "\r\n";
StreamReader reader = new StreamReader(response.GetResponseStream());
responseTextBox.Text += reader.ReadToEnd();
}
private static void SetRequestData(HttpWebRequest request, string data)
{
byte[] streamData = Encoding.ASCII.GetBytes(data);
request.ContentLength = streamData.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(streamData, 0, streamData.Length);
dataStream.Close();
}
但不幸的是在性反应,我得到的登录页面只有HTML源代码,似乎该cookie不包含会话ID。这是我的代码之后执行的所有请求也会返回登录页面的HTML源代码,所以我可以假设它不能正常登录。
But unfortunately in responce I get only HTML source code of login page and it seems that cookies don't contain session ID. All requests which I perform after that code also return HTML source of login page so I can assume that it does not login correctly.
任何人可以帮助我解决这个问题或给工作的例子?
Can anybody help me to solve that problem or give working example?
我想实现主要的是扫描新形象在NextGen的画廊WordPress插件。有没有XML-RPC这样做的方法是什么?
Main thing which I want to achieve is scanning for new images in Nextgen Gallery plugin for Wordpress. Is there XML-RPC way of doing that?
先谢谢了。
推荐答案
感谢大家。我设法如何使用插座时,使其只工作。 WordPress的发送几条设置Cookie 头,但的HttpWebRequest
只处理一个这样的报头的实例,这样一些饼干都将丢失。当使用插座,我可以得到所有需要的饼干和登录到管理面板。
Thanks to everyone. I managed how to make it work only when using sockets. Wordpress sends several Set-Cookie headers but HttpWebRequest
handles only one instance of such header so some cookies are lost. When using sockets I can get all needed cookies and login to admin panel.
这篇关于如何登录到WordPress编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!