且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用HttpWebRequest登录Wordpress

更新时间:2023-12-02 09:11:10

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

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

    }