我正在尝试使用 httwebrequest 和 c# 登录 wordpress 后台,但我做不到。我通过网络搜索,发现很多人都有同样的问题,但我找不到合适的解决方案。你能帮助我吗?提前致谢。
string adres = "http://www.site.com";
CookieContainer cookies = new CookieContainer();
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(adres + "wp-admin/");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
request.Method = "GET";
request.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
//POST
request = (HttpWebRequest)HttpWebRequest.Create(adres + "/wp-login.php");
request.Method = "POST";
request.CookieContainer = cookies;
string password = haslo;
string loginData = String.Format("loginform=&log=admin&pwd=password&testcookie=1&rememberme=forever&redirect_to=" + Uri.EscapeDataString(adres) + "wp-admin/&wp-submit=" + Uri.EscapeDataString("Zaloguj się"));
request.ContentType = "application/x-www-form-urlencoded";
byte[] loginDataBytes = Encoding.ASCII.GetBytes(loginData);
Stream postData = request.GetRequestStream();
postData.Write(loginDataBytes, 0, loginDataBytes.Length);
postData.Close();
response = (HttpWebResponse)request.GetResponse();
// NEXT GET
request = (HttpWebRequest)HttpWebRequest.Create(adres + "/wp-admin/");
request.Method = "GET";
request.CookieContainer = cookies;
HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();
Stream response1Stream = response1.GetResponseStream();
StreamReader reader1 = new StreamReader(response1Stream);
string input1 = reader1.ReadToEnd();
response1.Close();
richTextBox1.Text = input1;
我无法登录 -> 在第二个“GET”请求之后,我只得到登录表单,而不是 wordpress 后台主页。