您的位置:首页 > 理论基础 > 计算机网络

使用C#的HttpWebRequest模拟登陆网站

2014-09-13 10:34 931 查看
这篇文章是有关模拟登录网站方面的。

实现步骤;

启用一个web会话

发送模拟数据请求(POST或者GET)

获取会话的CooKie并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据。

我们以登录人人网为例,首先需要分析人人网登录时POST的数据格式,这个可以通过IE9中只带的F12快捷键,调出开发人员工具。如下图:



通过开始捕获得到POST的地址和POST的数据

POST数据:

email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476

POST地址:
http://www.renren.com/PLogin.do
下面就是代码示例来得到登录后页面(http://guide.renren.com/guide)的数据

HTMLHelper类

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Threading;namespaceTest{publicclassHTMLHelper{///<summary>///获取CooKie///</summary>///<paramname="loginUrl"></param>///<paramname="postdata"></param>///<paramname="header"></param>///<returns></returns>publicstaticCookieContainerGetCooKie(stringloginUrl,stringpostdata,HttpHeaderheader){HttpWebRequestrequest=null;HttpWebResponseresponse=null;try{CookieContainercc=newCookieContainer();request=(HttpWebRequest)WebRequest.Create(loginUrl);request.Method=header.method;request.ContentType=header.contentType;byte[]postdatabyte=Encoding.UTF8.GetBytes(postdata);request.ContentLength=postdatabyte.Length;request.AllowAutoRedirect=false;request.CookieContainer=cc;request.KeepAlive=true;

//提交请求Streamstream;
stream=request.GetRequestStream();
stream.Write(postdatabyte,0,postdatabyte.Length);
stream.Close();

//接收响应response=(HttpWebResponse)request.GetResponse();
response.Cookies=request.CookieContainer.GetCookies(request.RequestUri);
CookieCollectioncook=response.Cookies;
//Cookie字符串格式stringstrcrook=request.CookieContainer.GetCookieHeader(request.RequestUri);
returncc;
}
catch(Exceptionex)
{
throwex;
}
}

///<summary>
///获取html
///</summary>
///<paramname="getUrl"></param>
///<paramname="cookieContainer"></param>
///<paramname="header"></param>
///<returns></returns>publicstaticstringGetHtml(stringgetUrl,CookieContainercookieContainer,HttpHeaderheader)
{
Thread.Sleep(1000);
HttpWebRequesthttpWebRequest=null;
HttpWebResponsehttpWebResponse=null;
try
{
httpWebRequest=(HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer=cookieContainer;
httpWebRequest.ContentType=header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit=header.maxTry;
httpWebRequest.Referer=getUrl;
httpWebRequest.Accept=header.accept;
httpWebRequest.UserAgent=header.userAgent;
httpWebRequest.Method="GET";
httpWebResponse=(HttpWebResponse)httpWebRequest.GetResponse();
StreamresponseStream=httpWebResponse.GetResponseStream();
StreamReaderstreamReader=newStreamReader(responseStream,Encoding.UTF8);
stringhtml=streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
returnhtml;
}
catch(Exceptione)
{
if(httpWebRequest!=null)httpWebRequest.Abort();
if(httpWebResponse!=null)httpWebResponse.Close();
returnstring.Empty;
}
}
}

publicclassHttpHeader
{
publicstringcontentType{get;set;}
publicstringaccept{get;set;}
publicstringuserAgent{get;set;}
publicstringmethod{get;set;}
publicintmaxTry{get;set;}
}
}

测试用例:


HttpHeaderheader=newHttpHeader();

header.accept="image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/x-silverlight,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,application/x-ms-application,application/x-ms-xbap,application/vnd.ms-xpsdocument,application/xaml+xml,application/x-silverlight-2-b1,*/*";

header.contentType="application/x-www-form-urlencoded";

header.method="POST";

header.userAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;.NETCLR2.0.50727;.NETCLR3.0.04506.648;.NETCLR3.5.21022)";

header.maxTry=300;

stringhtml=HTMLHelper.GetHtml("http://guide.renren.com/guide",HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",

"email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476",header),header);

Console.WriteLine(html);

Console.ReadLine();



通过程序登录了网站后而直接进入登录后的页面。

首先还是发起一个启用一个web会话,然后发送模拟数据请求,获取会话的CooKie,再根据该CooKie将其写入到本地,通过程序直接打开登录后的页面。

该功能可用于无法修改第三方系统源代码而要做系统单点登录。

我们先在HTMLHelper类中添加一个方法:

///<summary>
///获取CookieCollection
///</summary>
///<paramname="loginUrl"></param>
///<paramname="postdata"></param>
///<paramname="header"></param>
///<returns></returns>publicstaticCookieCollectionGetCookieCollection(stringloginUrl,stringpostdata,HttpHeaderheader)
{
HttpWebRequestrequest=null;
HttpWebResponseresponse=null;
try
{
CookieContainercc=newCookieContainer();
request=(HttpWebRequest)WebRequest.Create(loginUrl);request.Method=header.method;
request.ContentType=header.contentType;
byte[]postdatabyte=Encoding.UTF8.GetBytes(postdata);
request.ContentLength=postdatabyte.Length;
request.AllowAutoRedirect=false;
request.CookieContainer=cc;
request.KeepAlive=true;

//提交请求Streamstream;
stream=request.GetRequestStream();
stream.Write(postdatabyte,0,postdatabyte.Length);
stream.Close();

//接收响应response=(HttpWebResponse)request.GetResponse();
response.Cookies=request.CookieContainer.GetCookies(request.RequestUri);
CookieCollectioncook=response.Cookies;
//Cookie字符串格式stringstrcrook=request.CookieContainer.GetCookieHeader(request.RequestUri);
returncook;
}
catch(Exceptionex)
{
throwex;
}
}

再根据获取的CookieCollection写入本地,并打开登录后的页面

[DllImport("wininet.dll",CharSet=CharSet.Auto,SetLastError=true)]
publicboolInternetSetCookie(stringlpszUrlName,stringlbszCookieName,stringlpszCookieData)
{
HttpHeaderheader=newHttpHeader();
header.accept="image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/x-silverlight,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,application/x-ms-application,application/x-ms-xbap,application/vnd.ms-xpsdocument,application/xaml+xml,application/x-silverlight-2-b1,*/*";
header.contentType="application/x-www-form-urlencoded";
header.method="POST";
header.userAgent="Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;.NETCLR2.0.50727;.NETCLR3.0.04506.648;.NETCLR3.5.21022)";
header.maxTry=300;
CookieCollectionmycookie=HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",
"email=aaa%40163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476",header);
foreach(Cookiecookieinmycookie)//将cookie设置为浏览的cookie{
InternetSetCookie("http://"+cookie.Domain.ToString(),cookie.Name.ToString(),cookie.Value.ToString()+";expires=Sun,22-Feb-209900:00:00GMT");
}
System.Diagnostics.Process.Start("http://guide.renren.com/guide");
}

这样即可直接通过程序打开登录后的页面:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: