抓取的封包
验证码
http://59.50.113.196:9080/security/servlet/AuthenCodeImage
封包如下
GET /security/servlet/AuthenCodeImage HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Host: 59.50.113.196:9080
Proxy-Connection: Keep-Alive
主页面
GET /personal/interfaces/hainan/index.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Host: 59.50.113.196:9080
Proxy-Connection: Keep-Alive
Cookie: JSESSIONID=36D4BA0C78E52DC65341E0FABD7D43BA
登陆页面
POST /personal/servlet/LoginServlet HTTP/1.1
Accept: */*
Referer: http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Proxy-Connection: Keep-Alive
Content-Length: 55
Host: 59.50.113.196:9080
Pragma: no-cache
Cookie: JSESSIONID=36D4BA0C78E52DC65341E0FABD7D43BAtxtUserAct=UserName&txtUserPwd=UserPwd&authencode=6181
验证码
http://59.50.113.196:9080/security/servlet/AuthenCodeImage
封包如下
GET /security/servlet/AuthenCodeImage HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Host: 59.50.113.196:9080
Proxy-Connection: Keep-Alive
主页面
GET /personal/interfaces/hainan/index.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Host: 59.50.113.196:9080
Proxy-Connection: Keep-Alive
Cookie: JSESSIONID=36D4BA0C78E52DC65341E0FABD7D43BA
登陆页面
POST /personal/servlet/LoginServlet HTTP/1.1
Accept: */*
Referer: http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)
Proxy-Connection: Keep-Alive
Content-Length: 55
Host: 59.50.113.196:9080
Pragma: no-cache
Cookie: JSESSIONID=36D4BA0C78E52DC65341E0FABD7D43BAtxtUserAct=UserName&txtUserPwd=UserPwd&authencode=6181
{
HttpRec myrec = new HttpRec();
string PostDate = "txtUserAct=69022565&txtUserPwd=69022565&authencode=";
PostDate += myrec.GetCode();
CookieCollection c = myrec.ReturnCookies();
foreach (Cookie cook in c)
{
this.richTextBox1.Text += cook.Domain+"--"+cook.Name+"---"+cook.Value; }
this.richTextBox1.Text +=PostDate+ myrec.LoginWeb(PostDate);
} public class HttpRec
{
private CookieCollection _cookCollection;
private string _Url = "http://59.50.113.196:9080/personal/servlet/LoginServlet";
private string _refUrl = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp";
private string _strErr; public CookieCollection ReturnCookies()
{
//if (_cookCollection != null)
//{
//}
return _cookCollection;
}
public string GetCode()
{
HttpWebRequest rqq = (HttpWebRequest)HttpWebRequest.Create("Http://59.50.113.196:9080/security/servlet/AuthenCodeImage");
rqq.Method = "Get";
rqq.KeepAlive = true;
// rqq.Referer = _refUrl;
if (rqq.CookieContainer == null)
{
rqq.CookieContainer = new CookieContainer();
}
HttpWebResponse rpp = (HttpWebResponse)rqq.GetResponse();
Image img = Bitmap.FromStream(rpp.GetResponseStream());
_cookCollection = rpp.Cookies; // img.Save(Environment.CurrentDirectory+"\\1.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
return ChuLiImg((Bitmap)img);
} public static string ChuLiImg(Bitmap img)
{
Rectangle[] RcArr = new Rectangle[4];
Rectangle RC1 = new Rectangle(4, 4, 6, 9);
Rectangle RC2 = new Rectangle(11, 4, 6, 9);
Rectangle RC3 = new Rectangle(18, 4, 6, 9);
Rectangle RC4 = new Rectangle(25, 4, 6, 9);
RcArr[0] = RC1;
RcArr[1] = RC2;
RcArr[2] = RC3;
RcArr[3] = RC4;
Bitmap[] bit = new Bitmap[4];
string CodeStr = string.Empty;
for (int i = 0; i < 4; i++)
{
string str =GetImgStr(ClearBg(img), RcArr[i]);
bit[i] = ((Bitmap)img).Clone(RcArr[i], img.PixelFormat);
if (str == "0111111111111111100000001000000011111111")
{
CodeStr += "0";
}
if (str == "0000000000110000011000001111111111111111")
{
CodeStr += "1";
}
if (str == "0100000111000011100001101000110011111000")
{
CodeStr += "2";
}
if (str == "0100000111000001100100001001000011111111")
{
CodeStr += "3";
}
if (str == "0000011000011010011000101111111111111111")
{
CodeStr += "4";
}
if (str == "0011100111111001110100001001000010011111")
{
CodeStr += "5";
}
if (str == "0011111101111111100100001001000011011111")
{
CodeStr += "6";
}
if (str == "1000000010000011100011111011110011110000")
{
CodeStr += "7";
}
if (str == "0111011111111111100010001000100011111111")
{
CodeStr += "8";
}
if (str == "0111100111111101100001001000010011111111")
{
CodeStr += "9";
}
}
return CodeStr; }
public static string GetImgStr(Bitmap Bigimg, Rectangle Rc)
{
Bitmap RCBmp = Bigimg.Clone(Rc, Bigimg.PixelFormat);
string str = "";
for (int w = 0; w < Rc.Width - 1; w++)
{
for (int h = 0; h < Rc.Height - 1; h++)
{ if (RCBmp.GetPixel(w, h).ToArgb() == Color.Black.ToArgb())
{ str += "1";
}
else
{
str += "0";
}
}
}
return str;
}
private static Bitmap ClearBg(Image Img)
{
Bitmap Smallimg = new Bitmap(Img);
for (int w = 0; w < Smallimg.Width - 1; w++)
{
for (int h = 0; h < Smallimg.Height - 1; h++)
{ if (Smallimg.GetPixel(w, h).ToArgb() < (Color.Black.ToArgb() * 0.4))
{ Smallimg.SetPixel(w, h, Color.Black);
}
else
{
Smallimg.SetPixel(w, h, Color.White);
}
}
}
return Smallimg;
}
{
string str = string.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_Url);//创建req
req.Accept = "*/*"; //接受任意文件
req.UserAgent = " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727)"; // 模拟使用IE在浏览
// req.KeepAlive = true;
req.Referer = "http://59.50.113.196:9080/personal/interfaces/hainan/index.jsp"; if (req.CookieContainer == null)
{
req.CookieContainer = new CookieContainer();
}
if (this._cookCollection != null)
{
System.Uri u = new Uri("http://59.50.113.196:9080/personal/servlet/LoginServlet");
//doenet处理cookie的bug:请求的服务器和cookie的Host必须一直,否则不发送或获取! //这里修改成一致!
foreach (System.Net.Cookie c in _cookCollection)
{
c.Domain = u.Host;
} req.CookieContainer.Add(_cookCollection);
} req.CookieContainer.Add(_cookCollection);
// req.CookieContainer.SetCookies(new Uri("59.50.113.196"),_cookCollection["d"].Value);
if (PostData != null & PostData.Length > 0)
{
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded"; byte[] b = Encoding.Default.GetBytes(PostData);
req.ContentLength = b.Length;
req.AutomaticDecompression = DecompressionMethods.GZip;
System.IO.Stream sw = null;
try
{
sw = req.GetRequestStream();
sw.Write(b, 0, b.Length);
}
catch (System.Exception ex)
{
this._strErr = ex.Message;
}
finally
{
if (sw != null) { sw.Close(); }
}
}
HttpWebResponse rep = null;
System.IO.StreamReader sr = null;
try
{
rep = (HttpWebResponse)req.GetResponse();
_cookCollection = rep.Cookies;
sr = new System.IO.StreamReader(rep.GetResponseStream(), Encoding.Default);
str = sr.ReadToEnd();
if (sr != null)
{
sr.Close();
}
}
catch (Exception e)
{ MessageBox.Show(e.Message); } return str;
}
}
直接到我的blog中复制
http://blog.csdn.net/yangyinzhu/archive/2008/07/16/2663509.aspx
新建一个按钮和一个多功能文本框就行了阿
麻烦高手帮忙了阿~实在是上火阿
第二,post发送时是用:application/x-www-form-urlencoded 编码,而你,直接使用Encoding.Default,这个遇到中文和特殊字符就要挂了。结贴吧!接分了。
1、打开监控软件,使用IE登录、提交等操作。就像你上边列举的那样,搞清楚:哪些cookie是访问哪个页面得到的。
2、最好把本地的IE里存储的cookie全部清除,在进行一下操作,确认一下。
再次确认的原因是因为,每次IE访问相关网站的时候,总会从本地缓存中查找相关cookie。
3、开始动手:编程。
3.1、构建一个CookieContainer,先把与目标网站相关的、本地储存的cookie得到并添加到这个CookieContainer 里边,这样省去一些麻烦,但是并非对每个网站都有效。
3.2把这个CookieContainer设置成与每次的HttpWebRequest相关。
3.3依次访问每个能得到提交页面所需的cookie来源的那个页面,这样,就会把所需的cookies全部得到了。
你的提交就会成功。
当然,别的细节问题你也要改动一下,比如:application/x-www-form-urlencoded 等。就目前你的问题来看,因为你没有访问index.jsp,所以,你的程序没法获得:
Cookie: JSESSIONID=36D4BA0C78E52DC65341E0FABD7D43BA
这个cookie.就你提供的资料来看,你的那个验证图片的请求响应没有相关的cookies设置和传送,我很是奇怪。
你是不是还漏了什么?
1.http://www.cnblogs.com/studyzy/archive/2008/05/06/1184341.html
2.http://www.cnblogs.com/studyzy/archive/2008/05/08/1187626.html
里面带有源代码摸拟CSDN的登录,测试并不可行,主要原因是其使用的类Http有问题,但思路没错。
3.http://blog.csdn.net/wo789/archive/2007/11/13/1882176.aspx
这篇文章里面的类WebClient写得不错,用3里面的类,用2里面的思路你的问题基本上就可以解决,我自己用这个类实现了CSDN的登录,另外还写了一个种子赠送软件:http://www.simplety.com.cn/china/download/SeedSend.rar
另外下面这篇文章讲Http协议头的,对你可能也有用:
4.http://hi.baidu.com/qqlyj/blog/item/0b2b8d351b1e491491ef3977.html