首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#验证码模拟登录的时候下载到的验证码异常

2014-01-08 
C#验证码模拟登录的时候下载到的验证码错误大家帮忙啊,我最近在搞C#验证码模拟登录,登录联通网上营业厅。ht

C#验证码模拟登录的时候下载到的验证码错误
大家帮忙啊,我最近在搞C#验证码模拟登录,登录联通网上营业厅。http://wwww.10010.com/login.jsp
我采用半自动的方式下载到验证码,然后自己填写,然后提交,可是返回值总说验证码错误,请大侠指教啊!


 //模拟登录
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string checkCode = textValidateCode.Text;
            string postUrl = "http://www.10010.com/login/userLogin.action";
            string postData = "loginInfo.userName=手机号&loginInfo.password=密码&loginInfo.check_code=" + checkCode + "&loginInfo.UniTokenRequest=null&loginInfo.returnURL=&loginInfo.areaNumber=&loginInfo.loginType=01&MobileUserName=手机号&loginInfo.identifyType=01&MobilePassword=密码&RandomPassword=&MobileCheckCode=" + checkCode + "&FixAreaNumber=%D6%D0%CE%C4%2F%C6%B4%D2%F4&FixUserName=%CE%DE%D0%E8%CA%E4%C8%EB%C7%F8%BA%C5%A3%A1&FixPassword=&FixCheckCode=%B5%E3%BB%F7%BB%F1%C8%A1&TeleAreanumber=%D6%D0%CE%C4%2F%C6%B4%D2%F4&TeleUserName=%CE%DE%D0%E8%CA%E4%C8%EB%C7%F8%BA%C5%A3%A1&TelePassword=&TeleCheckCode=%B5%E3%BB%F7%BB%F1%C8%A1&ADSLAreaNumber=%D6%D0%CE%C4%2F%C6%B4%D2%F4&ADSLUserName=%CE%DE%D0%E8%CA%E4%C8%EB%C7%F8%BA%C5%A3%A1&ADSLPassword=&ADSLCheckCode=%B5%E3%BB%F7%BB%F1%C8%A1&LANAreanumber=%D6%D0%CE%C4%2F%C6%B4%D2%F4&LANUserName=%CE%DE%D0%E8%CA%E4%C8%EB%C7%F8%BA%C5%A3%A1&LANPassword=&LANCheckCode=%B5%E3%BB%F7%BB%F1%C8%A1&WebUserName=%C4%FA%D7%A2%B2%E1%B5%C4%D3%CA%CF%E4&WebPassword=&WebCheckCode=%B5%E3%BB%F7%BB%F1%C8%A1";
            //string ydData="submitMode=2&ErrorUrl=..%2FbriefLogon.do&ReturnURL=www.sd.10086.cn%2Fportal%2Fgxhzq%2Findex.jsp&FieldID=1&entrance=IndexBrief&mobileNum=手机号&logonMode=1&servicePWD=******&randCode="+checkCode+"&smsRandomCode=";
            string strr = Http.GetHtml(postUrl, postData, aspcookie, out header);
            FileStream fst = new FileStream("D:\\0.htm", FileMode.Append);
            StreamWriter swt = new StreamWriter(fst, Encoding.GetEncoding("utf-8"));
            //写入
            swt.WriteLine(strr);
            swt.Close();
            fst.Close();

        }
        //下载验证码
        private void btnGetCode_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.10010.com/getImage.jsp");
            request.CookieContainer = co;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream resStream = response.GetResponseStream();//得到验证码数据流
            Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
            picValidateCode.Image = sourcebm;
        }
       CookieCollection Cookie = null;
        //获取登录页面cookie
        private void btnGetPage_Click(object sender, EventArgs e)
        {
            HttpWebRequest httpWebRequest;
            HttpWebResponse webResponse;
            //获取页面cookie
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.10010.com/login.jsp");
            co.SetCookies(new Uri("http://www.10010.com"), cookie);
            httpWebRequest.CookieContainer = co;
            webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            //getStream = webResponse.GetResponseStream();
            Cookie = webResponse.Cookies;
            header = webResponse.Headers.ToString();//返回头文件
            header = webResponse.Headers.Get("Set-Cookie");//返回Cookie


            MessageBox.Show(header);
        }


[解决办法]
验证码cookie不一致

CookieContainer cookie;
        private void btnGetCode_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.10010.com/getImage.jsp");
            request.CookieContainer = co;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream resStream = response.GetResponseStream();//得到验证码数据流
            Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
            picValidateCode.Image = sourcebm;
           cookie.Add(response.Cookies);   
  resStream.Close();
response.Close();
 
  }

 Http.GetHtml(postUrl, postData, aspcookie, out header);
把cookie传过去,设置登录附加cookie
 request.CookieContainer = cookie;
[解决办法]
不对,应该是把co传递过去,附加到模拟登录中。
[解决办法]
你是怎么看到验证码的?如果是通过浏览器看到的,那估计就是cookie的问题了,因为浏览器和你程序的cookie是不同的文件。你可以通过浏览器查看验证码的时候,先抓包看看和你登陆网站相关的cookie的名字和值都是多少,通过代码添加到你程序cookieContainer对象中,然后再按照你原先步骤试试

热点排行