可能标题写的不是很清楚,因为我真的不知道该写什么样的标题。
举例子说明吧
我想做个自动登陆xiaoyou.qq.com的工具,并且在登陆后能对以后的页面post数据。
通过截包知道,登陆时post的密码是经过md5的,至于是密码加上什么去MD5就不知道了,所以直接post进行登陆是不可行的。
那么我想到的方法是能不能用webbrowser显示登陆页面,填写好后点击登陆按钮,然后得到session,cooiks,有了这些以后的就都能post了。
或者还有其他的可行方法,望各位多多指教。
举例子说明吧
我想做个自动登陆xiaoyou.qq.com的工具,并且在登陆后能对以后的页面post数据。
通过截包知道,登陆时post的密码是经过md5的,至于是密码加上什么去MD5就不知道了,所以直接post进行登陆是不可行的。
那么我想到的方法是能不能用webbrowser显示登陆页面,填写好后点击登陆按钮,然后得到session,cooiks,有了这些以后的就都能post了。
或者还有其他的可行方法,望各位多多指教。
解决方案 »
- 急求:Could not load file or assembly 'ICSharpCode.SharpZipLib
- sql server 2000影响代理服务器的流量
- 服务端与客户端的数据双向同步设计问题
- WebClient类能实现创建文件夹,探测文件夹是否存在 删除文件删除文件夹吗?
- 请问C# 如何同步代码,和java中的cvs,svn类似的功能?
- 请问如何把tabcontrol中某一个tabpage变灰
- zedgraph怎么判断点在图的什么位置?
- c# 水晶报表程序的图表显示问题
- 请教“业务逻辑层”?
- 为什么按方向键不能出发MDI子窗口的KEYDOWN事件
- 打印
- 谁对byte,bit,BitArray这样的位操作比较熟悉的进来帮帮我
http://blog.csdn.net/menglingjun/archive/2007/09/18/1789788.aspx
加asp.net群交流83847699
在实际编程过程中,我们经常会遇到验证身份、程序升级网络投票会员模拟登陆等需要,C#给我们提供了以下的实现方法:
网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest这三个。以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:
HtmlElement ClickBtn =null;
if (e.Url.ToString().ToLower().IndexOf("xxx.htm") > 0) //登陆页面
{
HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count ; i++)
{
if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch (doc.All[i].Name)
{
case "userCtl":
doc.All[i].InnerText = "user01";
break;
case "passCt1":
doc.All[i].InnerText = "mypass";
break;
case "B1":
ClickBtn = doc.All[i]; //提交按钮
break;
}
}
}
ClickBtn.InvokeMember("Click"); //执行按扭操作
}
2、WebClient封装了HTTP的一些类,操作简单,相较于webBrowser,特点是可以自设代理,缺点是对COOKIE的控制
WebClient的运行全在后台,并且提供了异步操作的能力,这样很方便并发多个任务,然后等待结果的返回,再逐个处理。多任务异步调用的代码如下:
private void StartLoop(int ProxyNum)
{
WebClient [] wcArray = new WebClient[ProxyNum]; //初始化
for (int idArray = 0; idArray< ProxyNum;idArray++) {
wcArray[idArray] = new WebClient();
wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
try
{
......
wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
wcArray[idArray].OpenReadAsync(new Uri("/tp.asp?Id=129")); //打开WEB;
proxy = null;
}
catch
{
}
}
}
private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //取返回信息
.....
String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded");
((WebClient)sender).Headers.Add("Accept-Language", "zh-cn");
((WebClient)sender).Headers.Add("Cookie", cookie);
string postData = "......"
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成二进制数组
((WebClient)sender).UploadDataAsync(new Uri("/tp.asp?Id=129"), "POST", byteArray);
}
} private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
{
if (e.Error == null)
{
string returnMessage = Encoding.Default.GetString(e.Result);
......
}
}
3、HttpWebRequest较为低层,能实现的功能较多,Cookie操作也很简单
private bool PostWebRequest()
{
CookieContainer cc = new CookieContainer();
string pos tData = "user=" + strUser + "&pass=" + strPsd;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化 免费资源http://www.it55.com
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("/chk.asp"));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
......
}
做两点补充:1.登陆不能用post,因为,密码是经过加密以后才post出去的,至于是怎么加的密不知道,所以不能直接用post去登陆。
2.登陆完成后能对后续页面post数据,这个是重点!就是要保留登陆状态!!因为登陆操作不能用post,后续操作用post,之间的session正常情况下是不一样的,也就是说后面的post不能成功(session变了,服务器认为post非法)。现在就是想解决这个问题。