日期:2014-05-18  浏览次数:21103 次

关于HttpWebRequest模拟Web登陆和更新的WinForm程序问题
C# code
string strContent = "log=admin&pwd=221633&wp-submit=Log+In&redirect_to=http%3A%2F%2Flocalhost%2Fwordpress%2Fwp-admin%2F&testcookie=1";


            MessageBox.Show(strContent);
            byte[] bt = Encoding.ASCII.GetBytes(strContent);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/wordpress/wp-login.php");
            request.Method = "POST";

            request.CookieContainer = new CookieContainer();

            request.ContentType = "application/x-www-form-urlencoded";
            
            request.ContentLength = bt.Length;
            Stream sr = request.GetRequestStream();
            sr.Write(bt,0,bt.Length);
            sr.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            request.CookieContainer.Add(response.Cookies);


            if (response.StatusCode == HttpStatusCode.OK)
            {
                MessageBox.Show(response.StatusCode.ToString());
            }

            strContent = "post_title=111&content=111&tags_input=111&action=post-quickpress-publish&quickpress_post_ID=0&_wpnonce=e089f001a0&_wp_http_referer=%2Fwordpress%2Fwp-admin%2Findex.php";
            request = (HttpWebRequest)WebRequest.Create("http://localhost/wordpress/wp-admin/post.php");
            MessageBox.Show(strContent);
            bt = Encoding.ASCII.GetBytes(strContent);
            request.Method = "POST";
            request.CookieContainer = new CookieContainer();
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.ContentLength = bt.Length;
            sr = request.GetRequestStream();
            sr.Write(bt, 0, bt.Length);
            sr.Close();
            response = (HttpWebResponse)request.GetResponse();

            request.CookieContainer.Add(response.Cookies);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                MessageBox.Show(response.StatusCode.ToString());
            }


------解决方案--------------------
C# code

request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(response.Cookies);

------解决方案--------------------
其实我有个问题。为什么你自己可以连续回复4次。我记得只能回复3次



对于问题提供的建议

1.用httpanalyzerfullv 分析你自己的程序post 的数据 和 cookie 情况

与 正式情况的 post 数据 与 cookie是否相同


如果不相同看哪里不一样.然后改过来喽
------解决方案--------------------
request.CookieContainer.Add(response.Cookies);
....
request = (HttpWebRequest)WebRequest.Create("http://localhost/wordpress/wp-admin/post.php");
这时候,已经是一个新的webrequest了
request.CookieContainer = new CookieContainer();这时候cookie集合是空的,要把之前获取到的add进来才行,不然你刚才登陆那一步,等于没用~~