http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4154|回复: 3

[咨询站长] 带验证码 POST 服务端始终校验失败

[复制链接]
发表于 2015-4-7 11:28:43 | 显示全部楼层 |阅读模式
1金钱
post验证码始终失败,请问大家什么原因:

执行过程是:
1.下载验证码到本地,并获取cookie值。
2.附加第一步生成的cookie,post过去提交。
执行代码:

  
[C#] 纯文本查看 复制代码
string url = "http://www.plateno.com/imageToken?id=registry-checkcode&r=" + DateTime.Now.Ticks.ToString();
            string validatpath = "E:/123.jpg";
            string cookie = string.Empty;
            //下载验证码到本地并返回服务断的cookie
            bool b = Utility.HttpWebResponseUtility.DownValidateCodeImg(url, null, validatpath, out  cookie);
           
            //构造请求参数
            HttpHelper hh = new HttpHelper();
            HttpItem item = new HttpItem();
            item.Method = "Post";
            item.PostEncoding = Encoding.UTF8;
            string imgCode = DCHelper.GetImgValidateCode(validatpath, "liaoxinklx", "liaoxin349881223");
            Dictionary<string, string> postDic = new Dictionary<string, string>();
            postDic.Add("sellerId", "208624");
            postDic.Add("regSourceId", "12");
            postDic.Add("memberName", "赖世萍");
            postDic.Add("docNo", "440107196410190310");
            postDic.Add("password", "abc123");
            postDic.Add("passwordAgain", "abc123");
            postDic.Add("email", "");
            postDic.Add("checkcode", imgCode);
            postDic.Add("atk", "abcdefg");
            postDic.Add("agreeTerms", "on");
            item.PostDic = postDic;
            item.Cookie = cookie;
            item.ResultCookieType = ResultCookieType.CookieCollection;
            item.ContentType = "application/x-www-form-urlencoded";
            item.URL = "http://www.plateno.com/member/registry";
            HttpResult hr=hh.GetHtml(item);

下载验证码:
   public static bool DownValidateCodeImg(string url, Dictionary<string, string> parameters, string savePath,out string cookie)
        {
            cookie = string.Empty;
          
            if (parameters != null && parameters.Count > 0)
            {
                if (url.Contains("?"))
                {
                    url = url + "&" + BuildPostData(parameters);
                }
                else
                {
                    url = url + "?" + BuildPostData(parameters);
                }
            }
            bool bol = true;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            //属性配置   
            webRequest.KeepAlive = true;
            webRequest.AllowWriteStreamBuffering = true;
            webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            webRequest.MaximumResponseHeadersLength = -1;
            webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "GET";
            webRequest.Headers.Add("Accept-Language", "zh-cn");
            webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
            //webRequest.CookieContainer = cookCon;

            try
            {
                //获取服务器返回的资源   
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                      cookie = webResponse.Headers[HttpResponseHeader.SetCookie];
                    using (Stream sream = webResponse.GetResponseStream())
                    {
                        List<byte> list = new List<byte>();
                        while (true)
                        {
                            int data = sream.ReadByte();
                            if (data == -1)
                                break;
                            list.Add((byte)data);
                        }
                        File.WriteAllBytes(savePath, list.ToArray());
                    }
                }
            }
            catch (WebException ex)
            {
                bol = false;
            }
            catch (Exception ex)
            {
                bol = false;
            }
            return bol;
        }




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-4-7 11:47:38 | 显示全部楼层
看看你取的图片页面的Cookie是不是存在,也许Cookie不是在这里生成的。
回复

使用道具 举报

 楼主| 发表于 2015-4-7 15:37:14 | 显示全部楼层
站长苏飞 发表于 2015-4-7 11:47
看看你取的图片页面的Cookie是不是存在,也许Cookie不是在这里生成的。

验证码页面会产生一个cookie,  在注册页面加载的时候有同样的cookie,刷新验证码也没间覆盖的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-16 17:28

© 2014-2021

快速回复 返回顶部 返回列表