C#模擬登入總結
/// 登入
///
///
///
///
public static string Login(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = false;
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GBK");
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
strResult = e.ToString();
}
finally
{
if (res != null)
{
res.Close();
}
}
return strResult;
}
/// 獲取頁面HTML
///
///
///
///
public static string getPage(String url, String paramList)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Headers["If-None-Match"] = "36d0ed736e88c71:d9f";
req.Referer = "http://website/login.do";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.Default);
string strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}呼叫:
///
///
///
///
public static string Login(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = false;
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GBK");
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
strResult = e.ToString();
}
finally
{
if (res != null)
{
res.Close();
}
}
return strResult;
}
///
///
///
///
///
public static string getPage(String url, String paramList)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Headers["If-None-Match"] = "36d0ed736e88c71:d9f";
req.Referer = "http://website/login.do";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.Default);
string strResult = sr.ReadToEnd();
sr.Close();
return strResult;
}呼叫:
string postData = "userName=admin&password=pass&area=2006&Submit=%B5%C7+%C2%BC";
string strLogin, strResult;
strLogin = Login("http://website/login.do", postData);
strResult = getPage("http://website/tohjtree.do", "");
//輸出
this.webBrowser1.Document.Write(strResult);
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-662140/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python 爬蟲模擬登入方法彙總Python爬蟲
- c# 模擬網站登陸C#網站
- Python-模擬登入Python
- 【Java】——模擬登入實現Java
- 模擬賽總結(二)
- 模擬賽總結(三)
- 暑假模擬賽總結
- 7.13模擬賽總結
- 模擬賽總結補
- 利用Python模擬GitHub登入PythonGithub
- python使用Cookie模擬登入PythonCookie
- 模擬登入之web監控Web
- 實戰:Mailivery 模擬登入AI
- ocm模擬考試總結
- 10.2模擬賽總結
- 10月份模擬賽總結
- 11.2模擬賽總結
- 11.8模擬賽總結
- QQ模擬登入實現後篇
- 使用 mock 模擬登入介面資料Mock
- golang 模擬瀏覽器登入操作Golang瀏覽器
- 實戰模擬│JWT 登入認證JWT
- python模擬登入網易郵箱Python
- C#三種模擬自動登入和提交POST資訊的實現方法C#
- Python爬蟲之模擬知乎登入Python爬蟲
- Python 爬蟲(七)-- Scrapy 模擬登入Python爬蟲
- 爬蟲模擬登入—OAUTH的詳解爬蟲OAuth
- SQL Server模擬別的賬戶登入SQLServer
- 10.3 - AM - 模擬賽 總結
- 搜狗站長工具【post請求模擬登入】程式碼分享總結【批量提交搜狗收錄網址】
- 常見物件-模擬使用者登入案例物件
- 用python實現模擬登入人人網Python
- python模擬使用者登入某某網Python
- 為爬蟲獲取登入cookies: 使用Charles和requests模擬微博登入爬蟲Cookie
- 模擬弱網測試方法總結
- 實戰模擬│單點登入 SSO 的實現
- PID除錯軟體(C#、模擬、模擬)除錯C#
- 思科模擬器常用命令總結