Login Wordpress using HttpWebRequest

2020-08-02 11:41发布

问题:

I am trying to automate a few things in wordpress blog, using HttpWebRequest.

I have tried to get the login page "http://mywebsite.net/wp-admin"

and then try to post with login data on page

"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"

i am not able to login, wat i have discovered is that when using browser the cookies are sent to the server, but using HttpWebrequest the cookies are not sent on post, i am configured a cookiecontainer for the httpwebrequest and works fine other wise,..

and also on "post" the request host also changes to "www.mywebsite.net" and on "get" request it is "mywebsite.net"

Please can any one guide me through a solution.

回答1:

You should have been share some code also.However,what I think there are some cookies management issues. This is how I Manage cookies while posing data to the websites .You can use this management scheme code to login to your website.

     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";
            } 

    }


标签: c# asp.net http