.netはブログ園に登録して文章を発表します

3710 ワード

目的:用.NetコードログインNetそして文章を提出します
注意:asp.Netページのpostリクエストサーバ側がコミットします_VIEWSTATE、__EVENTVALLIDATIONは検証するので、要求時にこの2つのフィールドの内容を提出する必要があります.
 
取得__VIEWSTATE、__EVENTVALLIDATIONの2つのフィールドの内容を提出し、データ登録園を提出する
      //   url   html  

            var loginpagehtml = RequestHelper.GetResponseContent(url);



            //      __VIEWSTATE, __EVENTVALIDATION

            __VIEWSTATE = Regex.Match(loginpagehtml, reg1).Groups[1].Value;

            __EVENTVALIDATION = Regex.Match(loginpagehtml, reg2).Groups[1].Value;



            //       

            Dictionary<string, string> dicdata = new Dictionary<string, string>()

            {

                {"__VIEWSTATE",__VIEWSTATE},

                {"__EVENTVALIDATION", __EVENTVALIDATION},

                {"tbUserName", "username"},

                {"tbPassword", "password"},

                {"__EVENTTARGET",""},

                {"__EVENTARGUMENT", ""},

                {"btnLogin", "    "}

            };



            //          a=1&b=2  

            var postdata = dicdata.Select(dic => string.Format("{0}={1}", dic.Key, dic.Value)).Aggregate((a, b) => a + "&" + b);



            //      request,    response  

            var response = RequestHelper.GetResponse(url, postdata, (head) => { head.Method = "POST"; head.UserAgent = "Mozilla/5.0"; head.CookieContainer = cc; });

  
ログイン後、投稿ページに投稿します.コードは次のとおりです.
            //      

            var editpostpagehtml = RequestHelper.GetResponseContent("http://www.cnblogs.com/lyroge/admin/EditPosts.aspx?opt=1", null, (head) => { head.CookieContainer = cc; head.AllowAutoRedirect = true; });

            __VIEWSTATE = Regex.Match(editpostpagehtml, reg1).Groups[1].Value;            



            var dicdata = new Dictionary<string, string>()

            {

                {"__VIEWSTATE",__VIEWSTATE},            

                {"__EVENTTARGET",""},          

                {"__EVENTARGUMENT", ""},

                {"Editor$Edit$lkbPost", "  "},

                {"Editor$Edit$txbTitle", ".net               "},

                {"Editor$Edit$EditorBody", "<p>       </p>"},

                {"Editor$Edit$APOptions$APSiteHome$chkDisplayHomePage", "on"},

                {"Editor$Edit$Advanced$ckbPublished", "on"},

                {"Editor$Edit$Advanced$chkComments","on"},

                {"Editor$Edit$Advanced$chkMainSyndication", "on"},

                {"Editor$Edit$Advanced$txbEntryName",""},

                {"Editor$Edit$Advanced$txbExcerpt", ""},

                {"Editor$Edit$Advanced$txbTag",""},

                {"Editor$Edit$Advanced$tbEnryPassword", ""}

            };

            

            //           urlencode  

            var postdata = dicdata.Select(dic => string.Format("{0}={1}", System.Web.HttpUtility.UrlEncode(dic.Key), System.Web.HttpUtility.UrlEncode(dic.Value))).Aggregate((a, b) => a + "&" + b);

            

            var response = RequestHelper.GetResponse("http://www.cnblogs.com/lyroge/admin/EditPosts.aspx?opt=1", postdata, (head) =>

            {

                head.Method = "POST"; head.UserAgent = "Mozilla/5.0"; head.CookieContainer = cc; head.Referer = "http://www.cnblogs.com";

            });