模擬登陸

1.基礎幫助類php

public class WebAutoLogin
{
#region 屬性
/// <summary>
/// 登錄後返回的Html
/// </summary>
public static string ResultHtml
{
get;
set;
}
/// <summary>
/// 下一次請求的Url
/// </summary>
public static string NextRequestUrl
{
get;
set;
}
/// <summary>
/// 若要從遠程調用中獲取COOKIE必定要爲request設定一個CookieContainer用來裝載返回的cookies
/// </summary>
public static CookieContainer CookieContainer
{
get;
set;
}
/// <summary>
/// Cookies 字符創
/// </summary>
public static string CookiesString
{
get;
set;
}
#endregionweb

#region 方法
/// <summary>
/// 用戶登錄指定的網站
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="account"></param>
/// <param name="password"></param>
public static void PostLogin(string loginUrl, string account, string password)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
string postdata = "email=" + account + "&password=" + password + "&origURL=" + "http://www.renren.com/home" + "&domain=renren.com";//模擬請求數據,數據樣式能夠用FireBug插件獲得。
// string LoginUrl = "http://www.renren.com/PLogin.do";
request = (HttpWebRequest)WebRequest.Create(loginUrl);//實例化web訪問類
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";//數據提交方式爲POST
request.ContentType = "application/x-www-form-urlencoded"; //模擬頭
request.AllowAutoRedirect = false; // 不用需自動跳轉
//必須設置CookieContainer存儲請求返回的Cookies
if (CookieContainer != null)
{
request.CookieContainer = CookieContainer;
}
else
{
request.CookieContainer = new CookieContainer();
CookieContainer = request.CookieContainer;
}
request.KeepAlive = true;
//提交請求
byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabytes.Length;
Stream stream;
stream = request.GetRequestStream();
//設置POST 數據
stream.Write(postdatabytes, 0, postdatabytes.Length);
stream.Close();
//接收響應
response = (HttpWebResponse)request.GetResponse();
//保存返回cookie
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
CookiesString = strcrook;
//取下一次GET跳轉地址
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd();
sr.Close();
request.Abort();
response.Close();
//依據登錄成功後返回的Page信息,求出下次請求的url
//每一個網站登錄後加載的Url和順序不盡相同,如下兩步需根據實際狀況作特殊處理,從而獲得下次請求的URL
string[] substr = content.Split(new char[] { '"' });
NextRequestUrl = substr[1];
}
catch (WebException ex)
{
MessageBox.Show(string.Format("登錄時出錯,詳細信息:{0}", ex.Message));
}
}
/// <summary>
/// 獲取用戶登錄後下一次請求返回的內容
/// </summary>
public static string GetPage()
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(NextRequestUrl);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "GET";
request.KeepAlive = true;
request.Headers.Add("Cookie:" + CookiesString);
request.CookieContainer = CookieContainer;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
//設置cookie
CookiesString = request.CookieContainer.GetCookieHeader(request.RequestUri);
//取再次跳轉連接
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string ss = sr.ReadToEnd();
sr.Close();
request.Abort();
response.Close();
//依據登錄成功後返回的Page信息,求出下次請求的url
//每一個網站登錄後加載的Url和順序不盡相同,如下兩步需根據實際狀況作特殊處理,從而獲得下次請求的URL
string[] substr = ss.Split(new char[] { '"' });
NextRequestUrl = substr[1];
ResultHtml = ss;
return ss;
}
catch (WebException ex)
{
MessageBox.Show(string.Format("獲取頁面HTML信息出錯,詳細信息:{0}", ex.Message));
}
return "";
}
#endregion
}cookie

2.調用方法(模擬登陸人人網)app

WebAutoLogin.PostLogin("http://www.renren.com/PLogin.do", "人人網帳號", "人人網密碼");
WebAutoLogin.GetPage();
WebAutoLogin.GetPage();dom

3. 2345看圖王刷票post

用上篇文章的幫組類(模擬登陸人人網),下面是調用方法網站

WebAutoLogin1.CookieContainer = new System.Net.CookieContainer();
WebAutoLogin1.NextRequestUrl = "http://pic.2345.com/huodong/children/action.php?act=vote&&id=412&_=1433212086433";
WebAutoLogin1.CookiesString = "";
WebAutoLogin1.ResultHtml = "";
responseHtml = WebAutoLogin1.GetPage();url

相關文章
相關標籤/搜索