问题描述
c#模拟登录带验证码总提示验证码错误,这个网站以前没有加验证码的时候时可以正常模拟登录进去的。这是以前的代码:stringstr_ratk="";doubledttick=this.cConvertDateTimeInt(System.DateTime.Now);result=HttpHelper.GetHtml("http://www.plateno.com/p/nlogin.html?_="+dttick.ToString()+"");Regexrgeratk=newRegex(@"ratk""sw*=""w*"">",RegexOptions.Compiled);//ratk"sw*="w*">Matchmratk=rgeratk.Match(result);str_ratk=mratk.Groups[0].ToString().Trim();str_ratk=cGetStringBetween(str_ratk,"value="","">");result="";postData="fromType=0&username=13535127593&password=a111111&atk="+str_ratk.Trim()+"&j_expiredays=0&passwordType=0";result=HttpHelper.GetHtml("http://www.plateno.com/ajax_authentication",postData,true,HttpHelper.CookieContainer);网站加了验证码后参考了一些网友的做法,但总是提示“验证码错误”
解决方案
解决方案二:
验证码不会识别的话,那肯定是错误的。除非网站给留后门
解决方案三:
没打算用程序去识别验证码,我是想把验证码取到后显示出来,然后用手工输入验证吗,为什么这个网站总是提示验证码不正确?我试了一下其它的网站是可以的,不知道这个网站为什么不行//取回验证码privatevoidbtn_Pic_Click(objectsender,EventArgse){this.btn_Pic.Image=doGetImg("http://www.plateno.com/imageToken?id=lgCheckcode&r=0.6810257183387876",sc);}//下面是登录,这里面有个时间戳要处理一下privatevoidbtn_login_Click(objectsender,EventArgse){stringpostData="fromType=0&username=13535080156&password=a111111&lgCheckcode="+txt_code.Text.Trim()+"&atk=7d2f360f00fe4614bb503d9c2dd0f5a9&j_expiredays=0&passwordType=0";byte[]data=System.Text.Encoding.UTF8.GetBytes(postData);stringresult=doPost("http://www.plateno.com/ajax_authentication",data,sc,"utf-8","http://www.plateno.com/login.html?from=sso");}//publicstaticImagedoGetImg(stringUrl,TestCookiebCookie){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());myRequest.ServicePoint.Expect100Continue=true;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="GET";myRequest.Timeout=30000;myRequest.KeepAlive=true;//modifybyyangmyRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.1;Trident/4.0;SLCC2;.NETCLR2.0.50727;.NETCLR3.5.30729;.NETCLR3.0.30729;MediaCenterPC6.0)";myRequest.ContentType="application/x-www-form-urlencoded";HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);returnBitmap.FromStream(myResponse.GetResponseStream());}catch{returnnull;}}publicstaticstringdoPost(stringUrl,byte[]postData,TestCookiebCookie,StringencodingFormat,Stringreferer){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="POST";myRequest.Timeout=30000;myRequest.KeepAlive=true;if(referer!="")myRequest.Referer=referer;myRequest.Headers["Cache-control"]="no-cache";myRequest.Headers["Accept-Language"]="zh-cn";myRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;Trident/4.0;GTB7.4;GTB7.1;.NETCLR2.0.50727;.NETCLR3.0.4506.2152;.NETCLR3.5.30729;InfoPath.2)";myRequest.ContentType="application/x-www-form-urlencoded";myRequest.Accept="*/*";if(postData!=null){myRequest.ContentLength=postData.Length;StreamnewStream=myRequest.GetRequestStream();newStream.Write(postData,0,postData.Length);newStream.Close();}HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding(encodingFormat));stringoutdata=reader.ReadToEnd();reader.Close();if(!outdata.Contains("基础连接已经关闭:连接被意外关闭")&&!outdata.Contains("无法连接到远程服务器")&&!outdata.Contains("基础连接已经关闭:接收时发生错误。"))returnoutdata;elsereturn"基础连接已经关闭:连接被意外关闭";}catch(Exceptionex){if(!ex.Message.Contains("基础连接已经关闭:连接被意外关闭")&&!ex.Message.Contains("无法连接到远程服务器")&&!ex.Message.Contains("基础连接已经关闭:接收时发生错误。"))returnex.Message;elsereturn"基础连接已经关闭:连接被意外关闭";}}publicclassTestCookie{publicCookieContainermycookie=newCookieContainer();//定义cookie容器publicObjectobj=newObject();publicbyte[]byt=newbyte[1];publicvoidupcookie(CookieCollectioncookie){for(inti=0;i<cookie.Count;i++){mycookie.Add(cookie[i]);}obj=mycookie;byt=ObjectToBytes(obj);}/**////<summary>///将一个object对象序列化,返回一个byte[]///</summary>///<paramname="obj">能序列化的对象</param>///<returns></returns>publicstaticbyte[]ObjectToBytes(objectobj){using(MemoryStreamms=newMemoryStream()){IFormatterformatter=newBinaryFormatter();formatter.Serialize(ms,obj);returnms.GetBuffer();}}/**////<summary>///将一个序列化后的byte[]数组还原///</summary>///<paramname="Bytes"></param>///<returns></returns>publicobjectBytesToObject(byte[]Bytes){using(MemoryStreamms=newMemoryStream(Bytes)){IFormatterformatter=newBinaryFormatter();returnformatter.Deserialize(ms);}}publicCookieContainergetcookie(){returnmycookie;}}TestCookiesc=newTestCookie();
解决方案四:
一看就知道atk是验证码相关。你atk是固定了。你必须先去登录页面采集这个atk然后去获取验证码。然后提交。
解决方案五:
用fiddler抓包比较几次,看看post的字符串构造对不对如果构造正确,那么就检查headers,看看有没有防盗链处理
解决方案六:
获取验证码时有时会返回cookie,提交的时候要带上.代码好乱,看的头疼
解决方案七:
引用3楼wawd74520的回复:
一看就知道atk是验证码相关。你atk是固定了。你必须先去登录页面采集这个atk然后去获取验证码。然后提交。
这个atk的值我是为了调试方便固定了一下,实际上是每次都有去取这个atk的值,但把这个atk的值加上去后还是报验证码不正确,在帖子的内容里有加atk的,但不知道为什么总是不成功
解决方案八:
sc=newSinaCookie();doGet("http://www.plateno.com/p/nloginxxxxxxxxxx",sc);//登录界面去atkdoGetImg("http://www.plateno.com/imageToken?id=lgCheckcode&r=0.6810257183387876",sc);//验证码获取dopost。。。。登录类似这样。如果你有我的那些类。sc传进去Cookie会自动更新。。。
解决方案:
引用7楼wawd74520的回复:
sc=newSinaCookie();doGet("http://www.plateno.com/p/nloginxxxxxxxxxx",sc);//登录界面去atkdoGetImg("http://www.plateno.com/imageToken?id=lgCheckcode&r=0.6810257183387876",sc);//验证码获取dopost。。。。登录类似这样。如果你有我的那些类。sc传进去Cookie会自动更新。。。
我的大概思路也是这样子,但还是不行,能不能把你的类发一份给我参考一下?感谢!yourname386@163.com
解决方案:
publicstaticstringdoPost(stringUrl,byte[]postData,QQCookiebCookie,StringencodingFormat,Stringreferer,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}//myRequest.ServicePoint.Expect100Continue=false;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="POST";myRequest.Timeout=30000;myRequest.KeepAlive=true;//modifybyyangif(referer!="")myRequest.Referer=referer;myRequest.Headers["Cache-control"]="no-cache";//.CachePolicy=.c"no-cache";//["Cache-control:no-cache"]myRequest.Headers["Accept-Language"]="zh-cn";myRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;Trident/4.0;GTB7.4;GTB7.1;.NETCLR2.0.50727;.NETCLR3.0.4506.2152;.NETCLR3.5.30729;InfoPath.2)";myRequest.ContentType="application/x-www-form-urlencoded";myRequest.Accept="*/*";myRequest.ContentLength=postData.Length;//setRequestHeader(requestHearder,myRequest);StreamnewStream=myRequest.GetRequestStream();newStream.Write(postData,0,postData.Length);newStream.Close();//if(waitTime!=0)//Thread.Sleep(waitTime);HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding(encodingFormat));stringoutdata=reader.ReadToEnd();reader.Close();if(!outdata.Contains("基础连接已经关闭:连接被意外关闭")&&!outdata.Contains("无法连接到远程服务器")&&!outdata.Contains("基础连接已经关闭:接收时发生错误。"))returnoutdata;elsereturn"基础连接已经关闭:连接被意外关闭";}catch(Exceptionex){if(!ex.Message.Contains("基础连接已经关闭:连接被意外关闭")&&!ex.Message.Contains("无法连接到远程服务器")&&!ex.Message.Contains("基础连接已经关闭:接收时发生错误。"))returnex.Message;elsereturn"基础连接已经关闭:连接被意外关闭";}}publicstaticstringdoGet(stringUrl,QQCookiebCookie,StringencodingFormat,StringrefUrl,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}myRequest.ServicePoint.Expect100Continue=true;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="GET";if(refUrl!=null&&refUrl!=string.Empty)myRequest.Referer=refUrl;myRequest.Timeout=6000;myRequest.KeepAlive=true;//modifybyyangmyRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.1;Trident/4.0;SLCC2;.NETCLR2.0.50727;.NETCLR3.5.30729;.NETCLR3.0.30729;MediaCenterPC6.0)";myRequest.ContentType="application/x-www-form-urlencoded";//setRequestHeader(requestHeaders,myRequest);HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();//stringdddd=myResponse.Headers["Set-Cookie"];bCookie.upcookie(myResponse.Cookies);StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding(encodingFormat));stringoutdata=reader.ReadToEnd();reader.Close();returnoutdata;}catch(Exceptionex){returnex.Message;}}publicstaticImagedoGetImg(stringUrl,QQCookiebCookie,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}myRequest.ServicePoint.Expect100Continue=true;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="GET";//if(!CommonTool.isEmpty(refUrl))myRequest.Timeout=6000;myRequest.KeepAlive=true;//modifybyyangmyRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.1;Trident/4.0;SLCC2;.NETCLR2.0.50727;.NETCLR3.5.30729;.NETCLR3.0.30729;MediaCenterPC6.0)";myRequest.ContentType="application/x-www-form-urlencoded";HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);returnBitmap.FromStream(myResponse.GetResponseStream());}catch//(Exceptionex){returnnull;}}publicclassQQCookie{publicCookieContainermycookie=newCookieContainer();//定义cookie容器publicObjectobj=newObject();publicbyte[]byt=newbyte[1];publicvoidupcookie(CookieCollectioncookie){for(inti=0;i<cookie.Count;i++){mycookie.Add(cookie[i]);}obj=mycookie;byt=ObjectToBytes(obj);}/**////<summary>///将一个object对象序列化,返回一个byte[]///</summary>///<paramname="obj">能序列化的对象</param>///<returns></returns>publicstaticbyte[]ObjectToBytes(objectobj){using(MemoryStreamms=newMemoryStream()){IFormatterformatter=newBinaryFormatter();formatter.Serialize(ms,obj);returnms.GetBuffer();}}/**////<summary>///将一个序列化后的byte[]数组还原///</summary>///<paramname="Bytes"></param>///<returns></returns>publicobjectBytesToObject(byte[]Bytes){using(MemoryStreamms=newMemoryStream(Bytes)){IFormatterformatter=newBinaryFormatter();returnformatter.Deserialize(ms);}}publicCookieContainergetcookie(){returnmycookie;}}
解决方案:
你要先用HttpRequest访问取验证码地址,然后用这个Cookies取登录的页面,然后取到Hidden里的特殊值与Cookies最后把上面的Cookies一起打包发送请求就可以了。
解决方案:
引用9楼wawd74520的回复:
publicstaticstringdoPost(stringUrl,byte[]postData,QQCookiebCookie,StringencodingFormat,Stringreferer,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}//myRequest.ServicePoint.Expect100Continue=false;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="POST";myRequest.Timeout=30000;myRequest.KeepAlive=true;//modifybyyangif(referer!="")myRequest.Referer=referer;myRequest.Headers["Cache-control"]="no-cache";//.CachePolicy=.c"no-cache";//["Cache-control:no-cache"]myRequest.Headers["Accept-Language"]="zh-cn";myRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;Trident/4.0;GTB7.4;GTB7.1;.NETCLR2.0.50727;.NETCLR3.0.4506.2152;.NETCLR3.5.30729;InfoPath.2)";myRequest.ContentType="application/x-www-form-urlencoded";myRequest.Accept="*/*";myRequest.ContentLength=postData.Length;//setRequestHeader(requestHearder,myRequest);StreamnewStream=myRequest.GetRequestStream();newStream.Write(postData,0,postData.Length);newStream.Close();//if(waitTime!=0)//Thread.Sleep(waitTime);HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding(encodingFormat));stringoutdata=reader.ReadToEnd();reader.Close();if(!outdata.Contains("基础连接已经关闭:连接被意外关闭")&&!outdata.Contains("无法连接到远程服务器")&&!outdata.Contains("基础连接已经关闭:接收时发生错误。"))returnoutdata;elsereturn"基础连接已经关闭:连接被意外关闭";}catch(Exceptionex){if(!ex.Message.Contains("基础连接已经关闭:连接被意外关闭")&&!ex.Message.Contains("无法连接到远程服务器")&&!ex.Message.Contains("基础连接已经关闭:接收时发生错误。"))returnex.Message;elsereturn"基础连接已经关闭:连接被意外关闭";}}publicstaticstringdoGet(stringUrl,QQCookiebCookie,StringencodingFormat,StringrefUrl,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}myRequest.ServicePoint.Expect100Continue=true;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="GET";if(refUrl!=null&&refUrl!=string.Empty)myRequest.Referer=refUrl;myRequest.Timeout=6000;myRequest.KeepAlive=true;//modifybyyangmyRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.1;Trident/4.0;SLCC2;.NETCLR2.0.50727;.NETCLR3.5.30729;.NETCLR3.0.30729;MediaCenterPC6.0)";myRequest.ContentType="application/x-www-form-urlencoded";//setRequestHeader(requestHeaders,myRequest);HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();//stringdddd=myResponse.Headers["Set-Cookie"];bCookie.upcookie(myResponse.Cookies);StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding(encodingFormat));stringoutdata=reader.ReadToEnd();reader.Close();returnoutdata;}catch(Exceptionex){returnex.Message;}}publicstaticImagedoGetImg(stringUrl,QQCookiebCookie,stringProxyStr){try{HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(Url.ToString());if(ProxyStr!=""&&ProxyStr!=null){//设置代理WebProxyproxy=newWebProxy();proxy.Address=newUri(ProxyStr);myRequest.UseDefaultCredentials=true;myRequest.Proxy=proxy;}myRequest.ServicePoint.Expect100Continue=true;myRequest.CookieContainer=bCookie.mycookie;myRequest.Method="GET";//if(!CommonTool.isEmpty(refUrl))myRequest.Timeout=6000;myRequest.KeepAlive=true;//modifybyyangmyRequest.UserAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.1;Trident/4.0;SLCC2;.NETCLR2.0.50727;.NETCLR3.5.30729;.NETCLR3.0.30729;MediaCenterPC6.0)";myRequest.ContentType="application/x-www-form-urlencoded";HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();bCookie.upcookie(myResponse.Cookies);returnBitmap.FromStream(myResponse.GetResponseStream());}catch//(Exceptionex){returnnull;}}publicclassQQCookie{publicCookieContainermycookie=newCookieContainer();//定义cookie容器publicObjectobj=newObject();publicbyte[]byt=newbyte[1];publicvoidupcookie(CookieCollectioncookie){for(inti=0;i<cookie.Count;i++){mycookie.Add(cookie[i]);}obj=mycookie;byt=ObjectToBytes(obj);}/**////<summary>///将一个object对象序列化,返回一个byte[]///</summary>///<paramname="obj">能序列化的对象</param>///<returns></returns>publicstaticbyte[]ObjectToBytes(objectobj){using(MemoryStreamms=newMemoryStream()){IFormatterformatter=newBinaryFormatter();formatter.Serialize(ms,obj);returnms.GetBuffer();}}/**////<summary>///将一个序列化后的byte[]数组还原///</summary>///<paramname="Bytes"></param>///<returns></returns>publicobjectBytesToObject(byte[]Bytes){using(MemoryStreamms=newMemoryStream(Bytes)){IFormatterformatter=newBinaryFormatter();returnformatter.Deserialize(ms);}}publicCookieContainergetcookie(){returnmycookie;}}
加了你的类,还是验证码不正确QQCookiesc;stringstr_ratk="";privatevoidbtn_Pic_Click(objectsender,EventArgse){sc=newQQCookie();doubledttick=this.cConvertDateTimeInt(System.DateTime.Now);stringresult="";result="http://www.plateno.com/p/nlogin.html?_="+dttick.ToString()+"";result=doGet(result,sc,"utf-8","","");//登录界面去atk//Regexrgeratk=newRegex(@"ratk""sw*=""w*"">",RegexOptions.Compiled);//ratk"sw*="w*">Matchmratk=rgeratk.Match(result);str_ratk=mratk.Groups[0].ToString().Trim();str_ratk=cGetStringBetween(str_ratk,"value="","">");this.btn_Pic.Visible=false;this.btn_Pic.Image=doGetImg("http://www.plateno.com/imageToken?id=lgCheckcode&r=0.6810257183387876",sc,"");this.btn_Pic.Visible=true;}privatevoidbtn_tb_Click(objectsender,EventArgse){stringpostData="";stringresult="";strUser="13535080156";//13535127593userPWD="a111111";//postData="fromType=0&username="+strUser.Trim()+"&password="+userPWD+"&lgCheckcode="+txt_code.Text.Trim()+"&atk="+str_ratk.Trim()+"&j_expiredays=0&passwordType=0";postData="fromType=0&username=13535127593&password=a1&lgCheckcode="+txt_code.Text.Trim()+"&atk="+str_ratk.Trim()+"&j_expiredays=0&passwordType=0";byte[]data=System.Text.Encoding.UTF8.GetBytes(postData);result=doPost("http://www.plateno.com/ajax_authentication",data,sc,"utf-8","http://www.plateno.com/login.html?from=sso","");txt_textcode.Text=result;//提示:验证码不正确}
解决方案:
引用10楼wyd1520的回复:
你要先用HttpRequest访问取验证码地址,然后用这个Cookies取登录的页面,然后取到Hidden里的特殊值与Cookies最后把上面的Cookies一起打包发送请求就可以了。
这句怎么理解?然后取到Hidden里的特殊值与Cookies最后把上面的Cookies一起打包发送请求就可以了
解决方案:
这种网站没这么简单的。去把head什么的都弄弄。Cookie弄成stringCookie一个个取。