运用C#在采集时进行自动验证登录
来源:互联网 发布:软件测试 网络基础 编辑:程序博客网 时间:2024/05/18 09:14
最近有朋友在问我如何进行信息采集时对一些有用户或和密码验证的网站进行采集,刚好最近在项目中有运用到这个来进行验证登录,将部分代码发出来与大家分享 学习,这只是我在网上参考人家的做的一个雏形试用版,虽然能用,但是性能可能不是那么好。。目前只能对那些没有登录验证码的网站有效,如果有验证码的,呵 呵。那还得去写一个分析验证的类来进行Get.....
最近我的工作比较杂乱,也没有来得及整理,这些都是临时用了一下就搁在一边了,最近又忙着去学习Ubuntu、然后.Net+MySQL的一个项目,呵呵。还有星际。
首先打开网站,查看源文件,找到他的登录表单部分。
比如:
<form name="login" action="loginMain.jsp" method="POST" target="_top">
<table width="218" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50" height="28" class="hui"><div align="right">用户名:</div></td>
<td width="168" height="28"><input class="hui" id="username"
maxlength="40" size="23" name="username" id="username" /></td>
</tr>
<tr>
<td height="28" class="hui"><div align="right">密 码:</div></td>
<td height="28"><input class="hui" id="passwd"
maxlength="40" size="23" name="passwd" type="password" id="passwd" /></td>
</tr>
</table>
</form>
从以上表单可以看出,表单提交的方法是:POST,提交至loginMain.jsp处理,共有两个表单项即:username、passwd
下面是C#模仿登录程序:Login.cs
调用以上的方法来获取需要登录才能查看的内容。
sHtml即为你登录之后看到的内容。程序写的比较凌乱,如你有什么意见和不清楚的地方可以给我发邮件:hedongyang#gmail.com
(#请换为@就可以了)
最近我的工作比较杂乱,也没有来得及整理,这些都是临时用了一下就搁在一边了,最近又忙着去学习Ubuntu、然后.Net+MySQL的一个项目,呵呵。还有星际。
首先打开网站,查看源文件,找到他的登录表单部分。
比如:
<form name="login" action="loginMain.jsp" method="POST" target="_top">
<table width="218" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50" height="28" class="hui"><div align="right">用户名:</div></td>
<td width="168" height="28"><input class="hui" id="username"
maxlength="40" size="23" name="username" id="username" /></td>
</tr>
<tr>
<td height="28" class="hui"><div align="right">密 码:</div></td>
<td height="28"><input class="hui" id="passwd"
maxlength="40" size="23" name="passwd" type="password" id="passwd" /></td>
</tr>
</table>
</form>
从以上表单可以看出,表单提交的方法是:POST,提交至loginMain.jsp处理,共有两个表单项即:username、passwd
下面是C#模仿登录程序:Login.cs
using System;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
/**//// <summary>
/// 登录网站并获取Cookies
/// </summary>
/// <returns>成功登录的Cookie信息</returns>
public static CookieContainer Get_Login()
...{
CookieContainer cc = new CookieContainer();
string FormURL="http://blog.hnce.net/loginMain.jsp"; //处理表单的绝对URL地址
string FormData = "username=slick&passwd=hedy12345"; //表单需要提交的参数,注意改为你已注册的信息。
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(FormData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
request.Method = "POST"; //数据提交方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
//模拟一个UserAgent
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string WebContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return cc;
}
using System.Data;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
/**//// <summary>
/// 登录网站并获取Cookies
/// </summary>
/// <returns>成功登录的Cookie信息</returns>
public static CookieContainer Get_Login()
...{
CookieContainer cc = new CookieContainer();
string FormURL="http://blog.hnce.net/loginMain.jsp"; //处理表单的绝对URL地址
string FormData = "username=slick&passwd=hedy12345"; //表单需要提交的参数,注意改为你已注册的信息。
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(FormData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
request.Method = "POST"; //数据提交方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
//模拟一个UserAgent
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string WebContent = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return cc;
}
调用以上的方法来获取需要登录才能查看的内容。
CookieContainer cc = new CookieContainer();
cc = Login.Get_Login(); //获取登录Cookies
string PhotoClassURL = "http://blog.hnce.net/xxx.jsp";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.Default).ReadToEnd();
cc = Login.Get_Login(); //获取登录Cookies
string PhotoClassURL = "http://blog.hnce.net/xxx.jsp";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.Default).ReadToEnd();
sHtml即为你登录之后看到的内容。程序写的比较凌乱,如你有什么意见和不清楚的地方可以给我发邮件:hedongyang#gmail.com
(#请换为@就可以了)
以上为转自:
http://blog.csdn.net/hedongyang/archive/2007/05/15/1609379.aspx
- 运用C#在采集时进行自动验证登录
- 运用C#在采集时进行自动验证登录
- 运用C#在采集时进行自动验证登录
- 运用C#在采集时进行自动验证登录
- C# 在采集数据时的验证与登录处理
- C# 在采集数据时的验证与登录处理
- 使用Spring Security进行自动登录验证
- 使用Spring Security进行自动登录验证
- filter在登录上的运用(自动登录)
- 在C#中利用ActiveX控件进行视频采集
- 在C#中利用ActiveX控件进行视频采集
- 在C#中利用ActiveX控件进行视频采集
- (转)在C#中运用SharpZipLib和unrar进行解压缩
- html运用插件进行验证
- 登录时前台JS进行信息验证
- 【原创】c#如何实现RTU远程数据采集功能及RTU在水利工程中的运用
- c#如何实现RTU远程数据采集功能及RTU在水利工程中的运用
- Java Web开发如何在注册和登录时对用户名和密码进行验证?
- IBatis最新最全开发指南 - 通俗易懂IBAT教程(2)
- 加入csdn... ...
- VB.NET2003+水晶报表10打包过程(单机使用)
- 通过汇编代码分析C++的动态联编机制
- Java开源 开源工作流
- 运用C#在采集时进行自动验证登录
- Linux 内核的一些算法
- 宏跟函数及常量总结
- 如何下载Flv视频
- PR是与链接指向有关,与内容无关的东东
- asn.1 reference
- Windows CE下开发控制面板应用程序
- [转载]C语言程序设计入门学习六步曲
- 快速安装Trixbox(AAH)