HttpWebRequest 获取验证码进行识别,老是提示错误,验证码已经识别成功了

superkalar133 2012-06-06 05:20:16
HttpWebRequest 获取验证码进行识别,老是提示错误,验证码已经识别成功了, 可能是cookie的问题,求高手解答,本人愿意出500元RMB,跪求高手出现!qq476420016
...全文
520 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzhi86 2013-01-26
  • 打赏
  • 举报
回复
我也遇到兄弟同样的问题啊
superkalar133 2012-06-07
  • 打赏
  • 举报
回复
我把每次的cookie输出来了,大家看一下
第一次访问登录页面的cookie
Cookie:
PHPSESSID = d9f12f43b6e9507ddbdf7c8357631bd7
Domain: www.7daysinn.cn
Path: /
Port:
Secure: False
When issued: 2012-6-7 9:15:00
Expires: 0001-1-1 0:00:00 (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: PHPSESSID=d9f12f43b6e9507ddbdf7c8357631bd7

第二次获取验证码的cookie,貌似有3个,不知道为什么
Cookie:
www_safecode = 8C5DCEA2BE52B471EFD629A6216610B1
Domain: www.7daysinn.cn
Path: /safecode.php
Port:
Secure: False
When issued: 2012-6-7 9:15:04
Expires: 0001-1-1 0:00:00 (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: www_safecode=8C5DCEA2BE52B471EFD629A6216610B1
Cookie:
vcode_ct = 1339031798
Domain: www.7daysinn.cn
Path: /safecode.php
Port:
Secure: False
When issued: 2012-6-7 9:15:04
Expires: 0001-1-1 0:00:00 (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: vcode_ct=1339031798
Cookie:
PHPSESSID = d9f12f43b6e9507ddbdf7c8357631bd7
Domain: www.7daysinn.cn
Path: /
Port:
Secure: False
When issued: 2012-6-7 9:15:04
Expires: 0001-1-1 0:00:00 (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: PHPSESSID=d9f12f43b6e9507ddbdf7c8357631bd7

第三次,提交登录数据的时候的cookie
Cookie:
PHPSESSID = d9f12f43b6e9507ddbdf7c8357631bd7
Domain: www.7daysinn.cn
Path: /
Port:
Secure: False
When issued: 2012-6-7 9:15:00
Expires: 0001-1-1 0:00:00 (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: PHPSESSID=d9f12f43b6e9507ddbdf7c8357631bd7


输出cookie的代码是

void Print_Cookie(HttpWebResponse response)
{
foreach (Cookie cook in response.Cookies)
{
Console.WriteLine("Cookie:");
Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
Console.WriteLine("Domain: {0}", cook.Domain);
Console.WriteLine("Path: {0}", cook.Path);
Console.WriteLine("Port: {0}", cook.Port);
Console.WriteLine("Secure: {0}", cook.Secure);

Console.WriteLine("When issued: {0}", cook.TimeStamp);
Console.WriteLine("Expires: {0} (expired? {1})",
cook.Expires, cook.Expired);
Console.WriteLine("Don't save: {0}", cook.Discard);
Console.WriteLine("Comment: {0}", cook.Comment);
Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");

// Show the string representation of the cookie.
Console.WriteLine("String: {0}", cook.ToString());
}

}
superkalar133 2012-06-07
  • 打赏
  • 举报
回复
先访问登录页面,然后再获取验证码,然后再提交登录数据。用的都是一个cookie,但是还是提示验证码错误,求高手解答!
superkalar133 2012-06-07
  • 打赏
  • 举报
回复
void Post(string url, string indata, string strCookie)
{

string outdata = "";

CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合

//打开页面
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.7daysinn.cn/login.php");
//新建一个HttpWebRequest
myHttpWebRequest1.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest1.ContentLength = indata.Length;
//myHttpWebRequest1.Method = "";
myHttpWebRequest1.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest1.CookieContainer = myCookieContainer;

HttpWebResponse response1 = myHttpWebRequest1.GetResponse() as HttpWebResponse;
response1.Cookies = myHttpWebRequest1.CookieContainer.GetCookies(myHttpWebRequest1.RequestUri);
Print_Cookie(response1);
//打开页面
string url_sc = "http://www.7daysinn.cn/safecode.php";
HttpWebRequest myHttpWebRequest2 = (HttpWebRequest)WebRequest.Create(url_sc);
//新建一个HttpWebRequest
myHttpWebRequest2.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest2.ContentLength = indata.Length;
myHttpWebRequest2.Method = "GET";
myHttpWebRequest2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest2.CookieContainer = new CookieContainer();
myHttpWebRequest2.CookieContainer.Add(response1.Cookies);

HttpWebResponse response2 = myHttpWebRequest2.GetResponse() as HttpWebResponse;
response2.Cookies = myHttpWebRequest2.CookieContainer.GetCookies(myHttpWebRequest2.RequestUri);

Print_Cookie(response2);

System.Drawing.Image simg = System.Drawing.Image.FromStream(response2.GetResponseStream());
simg.Save("temp1.bmp");
Bitmap newimg = new Bitmap(simg);
pictureBox1.Image = simg;

//以下使用GetVcodeFromBuffer接口
StringBuilder Result = new StringBuilder('\0', 256);
string ImgPath = System.Environment.CurrentDirectory + "\\temp1.bmp";
FileStream fsMyfile = File.OpenRead(ImgPath);
int FileLen = (int)fsMyfile.Length;
byte[] Buffer = new byte[FileLen];
fsMyfile.Read(Buffer, 0, FileLen);
fsMyfile.Close();

if (GetVcodeFromBuffer(index, Buffer, FileLen, Result))
indata += Result.ToString();


HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest.CookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer.Add(response2.Cookies);
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata, 0, indata.Length);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection

Print_Cookie(myHttpWebResponse);

Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
wohaishiliumang 2012-06-06
  • 打赏
  • 举报
回复
no code you say a jb
bwangel 2012-06-06
  • 打赏
  • 举报
回复
你用HttpRequest获取验证码时,相当于点击了“重新获取验证码”,当然和本来的验证码不一样了。
你每请求一次验证码的地址,服务器都会重新生成一次。
500RMB, 请打给我的账号: 6222-12345678
花痴 2012-06-06
  • 打赏
  • 举报
回复
不详内情哟

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧