C# 使用HttpWebRequest登录Wordpress

C# 使用HttpWebRequest登录Wordpress,c#,asp.net,http,C#,Asp.net,Http,我正在尝试使用HttpWebRequest自动化wordpress博客中的一些内容 我已尝试获取登录页面”http://mywebsite.net/wp-admin“ 然后尝试在页面上发布登录数据 “”data=“log=admin&pwd=mypassword&wp submit=log+In&redirect\u=http%3A%2F%2Fmywebsite.net%2Fwp admin%2F&testcookie=1” 我无法登录,我发现当使用浏览器时,cookies会发送到服务器,但使用

我正在尝试使用
HttpWebRequest
自动化wordpress博客中的一些内容

我已尝试获取登录页面
”http://mywebsite.net/wp-admin“

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

“”data=“log=admin&pwd=mypassword&wp submit=log+In&redirect\u=http%3A%2F%2Fmywebsite.net%2Fwp admin%2F&testcookie=1”

我无法登录,我发现当使用浏览器时,cookies会发送到服务器,但使用HttpWebrequest时,cookies不会发送到post,我为HttpWebrequest配置了一个cookiecontainer,并在其他方面运行良好

此外,在“post”上,请求主机也更改为“www.mywebsite.net”和 在“获取”请求中,它是“mywebsite.net”


任何人都可以给我一个解决方案。

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

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

    }

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

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

    }

谢谢@codebusz。我用的是可可粉,但你的想法让我的想法更加光明。谢谢(让我先试试:)谢谢@CodeBuzz。我用的是可可粉,但你的想法让我的想法更加光明。谢谢(让我先试试:)