I m trying to login to wordpress backstage, using httwebrequest and c#, but I can t do it. I have searched via the web and found that a lot of people have the same problem, but I can t find the right solution. Can you help me? Thanks in advance.
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;
I can t login -> after second "GET" request, I just get the login form, not the wordpress backstage homepage.