1. 程式人生 > >使用C#的HttpWebRequest模擬登陸網站

使用C#的HttpWebRequest模擬登陸網站

很久沒有寫新的東西了,今天在工作中遇到的一個問題,感覺很有用,有種想記下來的衝動。

這篇文章是有關模擬登入網站方面的。

實現步驟;

  1. 啟用一個web會話
  2. 傳送模擬資料請求(POST或者GET)
  3. 獲取會話的CooKie 並根據該CooKie繼續訪問登入後的頁面,獲取後續訪問的頁面資料。

我們以登入人人網為例,首先需要分析人人網登入時POST的資料格式,這個可以通過IE9中只帶的F12快捷鍵,調出開發人員工具。如下圖:

 

通過開始捕獲得到POST的地址和POST的資料

POST資料:

[email protected]

&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類

複製程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Test
{
public class HTMLHelper
{
/// <summary>
/// 獲取CooKie
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
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;

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

//接收響應
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

CookieCollection cook = response.Cookies;
//Cookie字串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

return cc;
}
catch (Exception ex)
{

throw ex;
}
}

/// <summary>
/// 獲取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = 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();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
}

public class HttpHeader
{
public string contentType { get; set; }

public string accept { get; set; }

public string userAgent { get; set; }

public string method{get;set;}

public int maxTry { get; set; }
}
}
複製程式碼

 

測試用例:

複製程式碼
  HttpHeader header = new HttpHeader();
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; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
header.maxTry = 300;

string html = HTMLHelper.GetHtml("http://guide.renren.com/guide", HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",
"
[email protected]
&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類中新增一個方法:

複製程式碼
 1 /// <summary>
2 /// 獲取CookieCollection
3 /// </summary>
4 /// <param name="loginUrl"></param>
5 /// <param name="postdata"></param>
6 /// <param name="header"></param>
7 /// <returns></returns>
8 public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)
9 {
10 HttpWebRequest request = null;
11 HttpWebResponse response = null;
12 try
13 {
14 CookieContainer cc = new CookieContainer();
15 request = (HttpWebRequest)WebRequest.Create(loginUrl);
16 request.Method = header.method;
17 request.ContentType = header.contentType;
18 byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
19 request.ContentLength = postdatabyte.Length;
20 request.AllowAutoRedirect = false;
21 request.CookieContainer = cc;
22 request.KeepAlive = true;
23
24 //提交請求
25 Stream stream;
26 stream = request.GetRequestStream();
27 stream.Write(postdatabyte, 0, postdatabyte.Length);
28 stream.Close();
29
30 //接收響應
31 response = (HttpWebResponse)request.GetResponse();
32 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
33
34 CookieCollection cook = response.Cookies;
35 //Cookie字串格式
36 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
37
38 return cook;
39 }
40 catch (Exception ex)
41 {
42
43 throw ex;
44 }
45 }
複製程式碼


再根據獲取的CookieCollection寫入本地,並開啟登入後的頁面

複製程式碼
 1   [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
2
3 public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
4
5
6 HttpHeader header = new HttpHeader();
7 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, */*";
8 header.contentType = "application/x-www-form-urlencoded";
9 header.method = "POST";
10 header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
11 header.maxTry = 300;
12
13
14 CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",
15 "email=aaa%40163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header);
16
17
18 foreach (Cookie cookie in mycookie) //將cookie設定為瀏覽的cookie
19 {
20
21 InternetSetCookie(
22
23 "http://" + cookie.Domain.ToString(),
24
25 cookie.Name.ToString(),
26
27 cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");
28
29 }
30 System.Diagnostics.Process.Start("http://guide.renren.com/guide");
複製程式碼

這樣即可直接通過程式開啟登入後的頁面:

 

 

 

很久沒有寫新的東西了,今天在工作中遇到的一個問題,感覺很有用,有種想記下來的衝動。

這篇文章是有關模擬登入網站方面的。

實現步驟;

  1. 啟用一個web會話
  2. 傳送模擬資料請求(POST或者GET)
  3. 獲取會話的CooKie 並根據該CooKie繼續訪問登入後的頁面,獲取後續訪問的頁面資料。

我們以登入人人網為例,首先需要分析人人網登入時POST的資料格式,這個可以通過IE9中只帶的F12快捷鍵,調出開發人員工具。如下圖:

 

通過開始捕獲得到POST的地址和POST的資料

POST資料:

[email protected]&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類

複製程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Test
{
public class HTMLHelper
{
/// <summary>
/// 獲取CooKie
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
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;

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

//接收響應
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

CookieCollection cook = response.Cookies;
//Cookie字串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

return cc;
}
catch (Exception ex)
{

throw ex;
}
}

/// <summary>
/// 獲取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = 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();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
}

public class HttpHeader
{
public string contentType { get; set; }

public string accept { get; set; }

public string userAgent { get; set; }

public string method{get;set;}

public int maxTry { get; set; }
}
}
複製程式碼

 

測試用例:

複製程式碼
  HttpHeader header = new HttpHeader();
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; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
header.maxTry = 300;

string html = HTMLHelper.GetHtml("http://guide.renren.com/guide", HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",
"[email protected]&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();
複製程式碼