C#简单模拟用户登录类
为了实现模拟用户登录,忙活了两天了,先说一下我吃亏的地方。没有去深入理解,思维太单一。还是在此先感谢参考文章的作者!毕业论文要涉及到一个论坛采集系统,由于现在很多的论坛都要登录才能访问,具体说,我要实现的系统要登录一个网络课程的论坛,具体的页面没有帐号是不能访问的。
注:这里针对的是没有验证码的网站,以后有时间的话会把这个问题解决一下,不多说了,直接贴代码了:
程序结构:
class PostGetWebRequest
方法:PostWebRequest、 GetWebRequest();
要登录的网站的login.htm
<html >
<head>
<title></title>
</head>
<body>
<form id="form1" action="Default.aspx" method="post" name="form1">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 58px; height: 26px;">
<input type="text" name="userid" >
</td>
<td style="width: 100px; height: 26px;">
</td>
</tr>
<tr>
<td style="width: 58px; height: 26px;">
<input type="password" name="userpass" >
</td>
<td style="width: 100px; height: 26px;">
</td>
</tr>
<tr>
<td style="width: 58px; height: 26px">
</td>
<td style="width: 100px; height: 26px">
<input type="submit" value="submit1" name="submit1">
</td>
</tr>
<tr>
<td style="height: 26px">
</td>
<td style="height: 26px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
这里需要注意的参数:
form 的action ,post 的时候就是要post 这个action页面,如果是aspx 页面的话,就是本页了,当然aspx 是要复杂些的,多了两个viewstate 和__EVENTVALIDATION 两个参数。username :userid
password:userpass
submit name :submit1,value:submit1
引用别人的一段话:
httpWebRequest 提交数据后保持会话状态,我们在C# 中要知道下面的这些:
只要我们定义一个CookieContainer对象,用如下语句: CookieContainer cookie = new CookieContainer(); 然后把这个cookie附加到httpWebRequest的对象就行了.即myReq.CookieContainer = cookie;(myReq是httpWebRequest的对象).如果需要在别的httpWebRequester对象里保持会话状态,那么可以把 myReq.CookieContainer这个对象保存起来,如oldCookieContainer = myReq.CookieContainer并在再次进行网页请求时,把这个对象付给新的httpWebRequester对象。
登录的原理如下:
HTTP协议是一个无连接的协议,也就是说这次对话的内容和状态与上次的无关,为了实现和用户的持久交互,网站与浏览器之前在刚建立会话时将在服务器内存中建立一个Session,该Session标识了该用户(浏览器),每一个Session都有一个唯一的ID,第一次建立会话时服务器将生成的这个ID传给浏览器,浏览器在接下来的浏览中每一个发向服务器的请求中都将包含该SessionID,从而标识了自己的身份。
服务器上是使用内存来保存Session中的信息,那么浏览器又使用什么来保存服务器分配的这个SessionID了?对,是Cookie。在刚建立会话时浏览器向服务器的请求中将不包含SessionID在Cookie中,服务器就认为是一个全新的会话,从而在服务器上分配一段内存给该Session用,同时将该Session的ID在Http Header中使用Set-Cookie发送给浏览器。
---------------------------------------------------------------------
好,到这里前提条件都了解到差不多了。关于login.html 的action页面我们不去研究,只要知道是怎么post 数据的就可以了,如果想明确了解到话,可以使用http://www.svnhost.cn/Download/?k=httpwatch 上面的httpWatch 查看究竟有哪些postData.
C#类如下:
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.IO;5
using System.Net;6

7
namespace MyLogin8
{9
class PostGetWebRequest10
{11
private string userName;12
private string password;13
private string submit;14
private string loginUrl;15
private string locationUrl;16
private CookieContainer myCookieContainer;17
private HttpWebRequest request;18
private HttpWebResponse response;19
private Stream stream;20
private StreamReader streamReader;21
private string src="";22
private string postData;23
private string htmlUserName;24
private string htmlPassword;25
private string htmlSubmit;26

27
类的成员 87

88
public string PostWebRequest()89
{90
request = (HttpWebRequest)WebRequest.Create(LoginUrl);91

92
MyCookieContainer = new CookieContainer();93

94
//设置提交相关参数95
PostData = HtmlUserName + "=" + userName + "&" + HtmlPassword + "=" + Password + "&" + HtmlSubmit + "=" + Submit;//这里没有考虑中文的 System.Web.HttpUtility.UrlEncode(viewState);96
// PostData = "userid=GUEST&userpass=GUEST"; 97
//这里HtmlUserName 指的是控件的name 指,UserName指的是value,其他两个同98
ASCIIEncoding encoding = new ASCIIEncoding();99
byte[] data = encoding.GetBytes(PostData);//post 方式的编码100
request.Method="POST";//必须为大写,不然会出错101
request.ContentType="application/x-www-form-urlencoded";102
request.ContentLength=data.Length;103

104
request.KeepAlive = false;105
request.CookieContainer = MyCookieContainer;//把取服务器发给客户端跟session 想对应的cookie;相当于是一个容器106
107

108
//提交请求109
stream = request.GetRequestStream();110
stream.Write(data,0,data.Length);111
stream.Close();112
113
//接收114
response = (HttpWebResponse)request.GetResponse();115
streamReader = new StreamReader(response.GetResponseStream(),Encoding.UTF8);116

117
Src = streamReader.ReadToEnd();118
return Src; //单看提交页面的源代码 119
120
}121

122
public string GetWebRequest()123
{124
request=(HttpWebRequest)WebRequest.Create(LocationUrl);125
request.Method="GET";126
request.KeepAlive=false;127
request.CookieContainer = MyCookieContainer;128

129
//接收返回的页面130
response = (HttpWebResponse)request.GetResponse();131
stream = response.GetResponseStream();132
streamReader = new StreamReader (stream,Encoding.UTF8);133
// stream.Close();134
Src= streamReader.ReadToEnd();135
return Src;136
137
}138

139
140
}141
}142

调用页面代码:
PostGetWebRequest postGetWebRequest = new PostGetWebRequest() ;// 没有验证码的网站,登录页面html格式,客户端的2
postGetWebRequest.HtmlUserName="userid";3
postGetWebRequest.HtmlPassword="userpass";4
postGetWebRequest.HtmlSubmit = "submit1";5
postGetWebRequest.UserName = "GUEST";6
postGetWebRequest.Password = "GUEST";7
postGetWebRequest.Submit = "submit1";8
postGetWebRequest.LoginUrl = "http://××××/use_go.jsp";//这里非常关键,我在这里费了好久啊,虽然登录页面是"http://××××/";但处理页面是use_go.jsp,哎,.net 网站是在本页9
postGetWebRequest.LocationUrl = "http://×××?bbs_id=7822458&course_id=53601";10
string content1= postGetWebRequest.PostWebRequest();//着两个方法调用是有先后顺序的11
string content2 = postGetWebRequest.GetWebRequest();12

13
richTextBox1.Text = content1;14
richTextBox2.Text = content2;

浙公网安备 33010602011771号