使用HttpWebRequest登录WordPress

我正在尝试使用HttpWebRequest在wordpress博客中自动化一些东西。

我试图获取登录页面"http://mywebsite.net/wp-admin"

然后尝试在页面上发布登录数据

“ http://www.mywebsite.net/wp-login.php”data =“log = admin&pwd = mypassword&wp-submit = Log + In&redirect_to = http%3A%2F%2Fmywebsite.net%2Fwp-admin%2F&testcookie = 1”

我无法登录,我发现,当使用浏览器将cookie发送到服务器时,但是使用HttpWebrequest时,cookie不会发送到post上,我为httpwebrequest配置了一个cookiecontainer并且工作正常。 。

并且在“发布”请求主机也更改为“www.mywebsite.net”并在“获取”请求它是“mywebsite.net”

请任何人指导我完成解决方案。

你本来应该分享一些代码。但是,我认为有一些cookie管理问题。 这是我在向网站提供数据时管理cookie的方式。您可以使用此管理方案代码登录您的网站。

  public string postFormData(Uri formActionUrl, string postData) { //Make a HttpWebReguest first //set cookiecontainer gRequest.CookieContainer = new CookieContainer();// gCookiesContainer; //Manage cookies #region CookieManagement if (this.gCookies != null && this.gCookies.Count > 0) { gRequest.CookieContainer.Add(gCookies); } try { //logic to postdata to the form } catch (Exception ex) { Console.WriteLine(ex.Message); } //post data logic ends //Get Response for this request url try { gResponse = (HttpWebResponse)gRequest.GetResponse(); } catch (Exception ex) { Console.WriteLine(ex); } //check if the status code is ok if (gResponse.StatusCode == HttpStatusCode.OK) { //get all the cookies from the current request and add them to the response object cookies gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri); //check if response object has any cookies or not if (gResponse.Cookies.Count > 0) { //check if this is the first request/response, if this is the response of first request gCookies //will be null if (this.gCookies == null) { gCookies = gResponse.Cookies; } else { foreach (Cookie oRespCookie in gResponse.Cookies) { bool bMatch = false; foreach (Cookie oReqCookie in this.gCookies) { if (oReqCookie.Name == oRespCookie.Name) { oReqCookie.Value = oRespCookie.Value; bMatch = true; break; } } if (!bMatch) this.gCookies.Add(oRespCookie); } } } #endregion StreamReader reader = new StreamReader(gResponse.GetResponseStream()); string responseString = reader.ReadToEnd(); reader.Close(); return responseString; } else { return "Error in posting data"; } }