English 中文(简体)
Wordpresslogin httpwebrequest
原标题:Wordpress login httpwebrequest

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.

问题回答

几个月前,我也存在同样的问题,经过艰难的搜索,我找不到帮助我解决该问题的任何东西。

At the end, wrote my own code that solved the problem. It s a SSO (single sign on) between .NET Application and WordPress.

As is recurring, it has posted on my personal blog (sorry, but it s in spanish).

I hope it helps you ... http://sordalion.blogspot.com.es/2012/12/sso-aspnet-to-wordpress-aspnet-to.html

因此,有两处陷阱,一是,由于转头,没有建立标识,二是由于本地的厨师无法在所收<代码>Set-Cookies的头脑中 par一些。

To login into a Wordpress account via WebRequest you need to do the following two things:

1) Disable redirects on the POST request by setting AllowAutoRedirect = false

2) Parse the cookies manually from the Set-Cookies header and manually add them to the cookie container after the POST request succeeded.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签