基于c#实现网站的自动登陆
2014-05-24 20:03
459 查看
实现校园网的网关自动认证。
从网上找到了两种思路:一种是模拟网站的点击动作,实现自动登录。第二种是自动向登录目标发送登陆字符串。
下面是源码:
第一种:
//webBrowser1.Url =new Uri("http://);
////定义html元素 通过Name获取控件值
//HtmlElement tbUserid = webBrowser1.Document.All["DDDDD"];
//HtmlElement tbPasswd = webBrowser1.Document.All["upass"];
//HtmlElement btnSubmit = webBrowser1.Document.All["0MKKey"];
//////定义html元素 通过ID获取控件值 (用户名 密码 登录按钮)
////HtmlElement tbUserid = webBrowser1.Document.GetElementById("DDDDD");
////HtmlElement tbPasswd = webBrowser1.Document.GetElementById("upass");
////HtmlElement btnSubmit = webBrowser1.Document.GetElementById("0MKKey");
////三个元素其一为空返回 加载后才执行赋值 否则会出现为null值的崩溃错误
//if (tbUserid == null || tbPasswd == null || btnSubmit == null)
//{
// return;
//}
////设置元素value属性值 (用户名 密码值)
//tbUserid.SetAttribute("value","111111");
//tbPasswd.SetAttribute("value", "111111");
////执行元素的方法:如click submit
//btnSubmit.InvokeMember("click");
第二种:
string username = "11";//用户名
string password = "1111";//密码
//新建一个用于保存cookies的容器
CookieContainer container = new CookieContainer();
//拼接post数据DDDDD=11&upass=1111&0MKKey=%C1%AC%BD%D3%CD%F8%C2%E7
string postData = ("DDDDD=" + username);
postData += ("&upass=" + password);
postData += ("&0MKKey=%C1%AC%BD%D3%CD%F8%C2%E7");
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://");
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.CookieContainer = container; //返回的cookie会附加在这个容器里面
//发送数据
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
////以下俩句不可缺少
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = container.GetCookies(request.RequestUri);
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
textBox3.Text = content;
从网上找到了两种思路:一种是模拟网站的点击动作,实现自动登录。第二种是自动向登录目标发送登陆字符串。
下面是源码:
第一种:
//webBrowser1.Url =new Uri("http://);
////定义html元素 通过Name获取控件值
//HtmlElement tbUserid = webBrowser1.Document.All["DDDDD"];
//HtmlElement tbPasswd = webBrowser1.Document.All["upass"];
//HtmlElement btnSubmit = webBrowser1.Document.All["0MKKey"];
//////定义html元素 通过ID获取控件值 (用户名 密码 登录按钮)
////HtmlElement tbUserid = webBrowser1.Document.GetElementById("DDDDD");
////HtmlElement tbPasswd = webBrowser1.Document.GetElementById("upass");
////HtmlElement btnSubmit = webBrowser1.Document.GetElementById("0MKKey");
////三个元素其一为空返回 加载后才执行赋值 否则会出现为null值的崩溃错误
//if (tbUserid == null || tbPasswd == null || btnSubmit == null)
//{
// return;
//}
////设置元素value属性值 (用户名 密码值)
//tbUserid.SetAttribute("value","111111");
//tbPasswd.SetAttribute("value", "111111");
////执行元素的方法:如click submit
//btnSubmit.InvokeMember("click");
第二种:
string username = "11";//用户名
string password = "1111";//密码
//新建一个用于保存cookies的容器
CookieContainer container = new CookieContainer();
//拼接post数据DDDDD=11&upass=1111&0MKKey=%C1%AC%BD%D3%CD%F8%C2%E7
string postData = ("DDDDD=" + username);
postData += ("&upass=" + password);
postData += ("&0MKKey=%C1%AC%BD%D3%CD%F8%C2%E7");
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://");
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.CookieContainer = container; //返回的cookie会附加在这个容器里面
//发送数据
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
////以下俩句不可缺少
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = container.GetCookies(request.RequestUri);
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
textBox3.Text = content;
相关文章推荐
- [导入]C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c# 实现网页上用户自动登陆|asp.net 模拟网站登录
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c# 实现网页上用户自动登陆|asp.net 模拟网站登录
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 基于读取车载RFID标签自动开关车库门c#算法实现
- 基于C#实现的开源自动更新程序
- c# winform实现网页上用户自动登陆,模拟网站登录
- c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇
- python实现带验证码网站的自动登陆实现代码
- 【转】详解抓取网站,模拟登陆,抓取动态网页的原理和实现(Python,C#等)
- c#实现对登陆信息的反馈,实现对网站登录密码的扫描
- c#模拟网页实现12306登陆、自动刷票、自动抢票完全篇
- 基于COM技术的ATM自动存款机实现(C#版)
- c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇(转)