我需要以编程方式在wordpress管理面板中执行某些操作,但无法管理如何使用C#和HttpWebRequest登录到Wordpress.
这是我做的:
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源代码,因此我可以假设它无法正确登录.
任何人都可以帮助我解决这个问题或举例说明吗?
我想要实现的主要功能是在Wordpress的Nextgen Gallery插件中扫描新图像.是否有XML-RPC这样做的方式?
提前致谢.
由于WordPress实现了重定向,因此离开页面(重定向)会阻止webrequest获取正确的cookie.
为了获得相关的cookie,必须防止重定向.
request.AllowAutoRedirect = false;
而不是使用cookie-conatainer登录.
请参阅以下代码:(基于Albahari的C#书中的示例)
string loginUri = "http://www.someaddress.com/wp-login.php"; string username = "username"; string password = "pass"; string reqString = "log=" + username + "&pwd=" + password; byte[] requestData = Encoding.UTF8.GetBytes(reqString); CookieContainer cc = new CookieContainer(); var request = (HttpWebRequest)WebRequest.Create(loginUri); request.Proxy = null; request.AllowAutoRedirect = false; request.CookieContainer = cc; request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = requestData.Length; using (Stream s = request.GetRequestStream()) s.Write(requestData, 0, requestData.Length); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { foreach (Cookie c in response.Cookies) Console.WriteLine(c.Name + " = " + c.Value); } string newloginUri = "http://www.someaddress.com/private/"; HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri); newrequest.Proxy = null; newrequest.CookieContainer = cc; using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse()) using (Stream resSteam = newresponse.GetResponseStream()) using (StreamReader sr = new StreamReader(resSteam)) File.WriteAllText("private.html", sr.ReadToEnd()); System.Diagnostics.Process.Start("private.html");