我用下面代码下载到的验证码
HttpWebRequest h = (HttpWebRequest)HttpWebRequest.Create("http://9.52ms.org/vpn/include/vdimgck.php");
          
            HttpWebResponse response = (HttpWebResponse)h.GetResponse();
            pictureBox1.Image = new Bitmap(response.GetResponseStream());
            CookieContainer cookie = new CookieContainer();
        cookie.Add(response.Cookies);
用的下面这个方法调用模拟post提交
 string html   = HttpHelper.GetHtml("http://9.52ms.org/vpn/useradmin/user_self_new_save.php", null, "POST", "ioBB=&regsdid=&chksdid=false&hidRecName001=&hidRecName002=&hidRecRule001=&hidRecRule002=&agent4IpCheck=checkIp&txtRegisterFrom=0&txtRegisterZone=0&sessionid=lr1c1w55vih4et45ct511krb&goldtype=&partner=&apptype=&agent=&ctype=&hidExinfo=&qqaccount=&CUSTOM_REG_EVN=&ptType=0&UserID=aab2" + i + "&Upassword=123123&Upassword2=123123&[email protected]&User_True=&User_QQ=&idcard=&Description=&ThisNo="+textBox1.Text+"&btnSubmit=+%E7%AB%8B%E5%8D%B3%E6%B3%A8%E5%86%8C91vpn%E8%B4%A6%E5%8F%B7+");
        MessageBox.Show(html);
下面是post提交的方法。输入的验证码不一样,提示错误,请大家指示。为什么?难道这样模拟获取的验证码,没有效果么?
听说是什么cookies的问题?怎么整?

 public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
        {
            cookieContainer = cookieContainer ?? new CookieContainer();            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;            try
            {
                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.Referer = referer;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = method;
                httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;                if (method.ToUpper() == "POST")
                {
                    byte[] byteRequest = Encoding.Default.GetBytes(postData);
                    Stream stream = httpWebRequest.GetRequestStream();
                    stream.Write(byteRequest, 0, byteRequest.Length);
                    stream.Close();
                }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                string html = streamReader.ReadToEnd();                streamReader.Close();
                responseStream.Close();                httpWebRequest.Abort();
                httpWebResponse.Close();                return html;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

解决方案 »

  1.   

    先声明一个CookieContainer cookieContainer
    获取验证码的时候和提交注册的时候把这个CookieContainer 带上就可以了。不需要对cookieContainer和cookie做任何操作。
    (发送请求时回自动带上已经存在的cookie,接受response的时候会自动把set-cookie写入cookieContainer中)注意你获取验证码的时候和提交注册请求的时候,需要带上同一个cookieContainer
      

  2.   


        public class HttpHelper
        {
            public static string contentType = "application/x-www-form-urlencoded";
            public static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
            public static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)";
            public static string referer = "http://9.52ms.org/vpn/useradmin/user_self_new.php";        /// <summary>
            /// 获取字符流
            /// </summary>
            /// <param name="url"></param>
            /// <param name="cookieContainer"></param>
            /// <returns></returns>
            public static Stream GetStream(string url, CookieContainer cookieContainer)
            {
                HttpWebRequest httpWebRequest = null;
                HttpWebResponse httpWebResponse = null;            try
                {
                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                    httpWebRequest.CookieContainer = cookieContainer;
                    httpWebRequest.ContentType = contentType;
                    httpWebRequest.Referer = referer;
                    httpWebRequest.Accept = accept;
                    httpWebRequest.UserAgent = userAgent;
                    httpWebRequest.Method = "GET";
                    httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    Stream responseStream = httpWebResponse.GetResponseStream();
                    
                    return responseStream;
                }
                catch (Exception)
                {
                    return null;
                }
            }        /// <summary>
            /// 获取HTML
            /// </summary>
            /// <param name="url"></param>
            /// <param name="cookieContainer"></param>
            /// <param name="method">GET or POST</param>
            /// <param name="postData">like "username=admin&password=123"</param>
            /// <returns></returns>
            public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
            {
                cookieContainer = cookieContainer ?? new CookieContainer();            HttpWebRequest httpWebRequest = null;
                HttpWebResponse httpWebResponse = null;            try
                {
                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                    httpWebRequest.CookieContainer = cookieContainer;
                    httpWebRequest.ContentType = contentType;
                    httpWebRequest.Referer = referer;
                    httpWebRequest.Accept = accept;
                    httpWebRequest.UserAgent = userAgent;
                    httpWebRequest.Method = method;
                    httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;                if (method.ToUpper() == "POST")
                    {
                        byte[] byteRequest = Encoding.Default.GetBytes(postData);
                        Stream stream = httpWebRequest.GetRequestStream();
                        stream.Write(byteRequest, 0, byteRequest.Length);
                        stream.Close();
                    }                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    string cookie_string = httpWebResponse.Headers.Get("Set-Cookie");
                    cookie_string += "0";
                    Stream responseStream = httpWebResponse.GetResponseStream();
                    StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                    string html = streamReader.ReadToEnd();                streamReader.Close();
                    responseStream.Close();                httpWebRequest.Abort();
                    httpWebResponse.Close();                return html;
                }
                catch (Exception)
                {
                    return string.Empty;
                }
            }
        }
    调用:            CookieContainer cookies = new CookieContainer();
                Stream imageStream = HttpHelper.GetStream("http://9.52ms.org/vpn/include/vdimgck.php?rand=0.2047209680588571", cookies);
                //image1.Image=Image.FromStream(imageStream);            HttpHelper.GetHtml("http://9.52ms.org/vpn/useradmin/user_self_new_save.php", cookies, "POST", "ioBB=&regsdid=&chksdid=false&hidRecName001=&hidRecName002=&hidRecRule001=&hidRecRule002=&agent4IpCheck=checkIp&txtRegisterFrom=0&txtRegisterZone=0&sessionid=lr1c1w55vih4et45ct511krb&goldtype=&partner=&apptype=&agent=&ctype=&hidExinfo=&qqaccount=&CUSTOM_REG_EVN=&ptType=0&UserID=asdfa3sdf32&Upassword=password&Upassword2=password&[email protected]&User_True=&User_QQ=&idcard=&Description=&ThisNo=fbns&btnSubmit=+%E7%AB%8B%E5%8D%B3%E6%B3%A8%E5%86%8C91vpn%E8%B4%A6%E5%8F%B7+");
    调用的时候注意使用同一个CookieContainer,注意替换post参数中的值。
      

  3.   

    楼上的应该可以, 不正确的原因,一定是 Cookie 不在一个Domain中,所以,你要把 Cookie装载到 HttpWebRequest中 ,
      

  4.   

    我试了一下参数中ThisNo就是验证码,替换这个值就可以注册成功了:
    UserID=asdfa3sdf32
    Upassword=password
    Upassword2=password
    [email protected]
    ThisNo=fbns
      

  5.   

    谢谢兄台,学习了,我错的原因只要把空cookie给进去就可以吧?
    我看你写的是get提交也行?
    现在没再家,没办法测试,不过还是谢谢了
    希望您有时间能说下我的错误
      

  6.   


    我已经帮你测试过了,获取验证码用的是GET,提交注册用的是POST。
      

  7.   

    HttpWebRequest h = (HttpWebRequest)HttpWebRequest.Create("http://9.52ms.org/vpn/include/vdimgck.php");HttpWebResponse response = (HttpWebResponse)h.GetResponse();
    pictureBox1.Image = new Bitmap(response.GetResponseStream());我这样不行的原因是什么,您知道么?这样是他的验证码没放到session里或者cookie里?
    只是单纯的获取到图片,但是没放到他验证的地方 吧?
      

  8.   


    cookie丢了。cookie里面有个PHPSessionId用来记录同一个会话。
    你获取验证码的时候的cookie丢掉了,然后提交注册的时候他就不认那个验证码了。