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());
            }

解决方案 »

  1.   

    我觉得是cookie那个地方的问题,但是确实不清楚是哪的问题
      

  2.   


    request.CookieContainer = new CookieContainer();
    request.CookieContainer.Add(response.Cookies);这两个地方有错。
    应该是//定义一个全局变量 mycookie
    private CookieContainer _mycookie = new CookieContainer();
    public CookieContainer mycookie { get { return _mycookie; } set { _mycookie = value; } }
    //mycookie,应该处于一个不会湮灭的进程中;
    //先要GET下,获取Cookie;而后直接使用就行了;
    request.CookieContainer = mycookie;
    //不需要CookieContainer.Add();更何况是Add一个空的Cookies;
    request.CookieContainer = mycookie;
    //这样就行了,CookieContainer,自己自动添加和更新的。
      

  3.   

    其实我有个问题。为什么你自己可以连续回复4次。我记得只能回复3次对于问题提供的建议1.用httpanalyzerfullv   分析你自己的程序post  的数据  和  cookie 情况与  正式情况的  post 数据   与 cookie是否相同
    如果不相同看哪里不一样.然后改过来喽
      

  4.   

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

  5.   

    先发送请求才有cookie呀,没发送请求哪来的cookie