日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie

發(fā)布時(shí)間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由于種種原因,我們有時(shí)需要從互聯(lián)網(wǎng)上抓取一些資料,有些頁(yè)面可以直接打開,而有些頁(yè)面必登錄之后才能打開。本文介紹的是使用 HttpWebRequest 和 HttpWebResponse 自動(dòng)填寫提交 ASP.NET 表單并保持 Session 和 Cookie 的一個(gè)完整的例子。

這里涉及到3個(gè)頁(yè)面:MyLogin.aspx,LoginOK.htm,Default.aspx:
1)MyLogin.aspx 頁(yè)面

MyLogin.aspx 頁(yè)面是登錄頁(yè)面,如果用戶名和密碼正確會(huì)生成 Session 和 Cookie(LoginSession、LoginCookie),然后轉(zhuǎn)向 LoginOK.htm 頁(yè)面。

2)LoginOK.htm 頁(yè)面

LoginOK.htm 頁(yè)面是一個(gè)跳轉(zhuǎn)頁(yè)面,幾秒鐘后會(huì)自動(dòng)跳轉(zhuǎn)到 Default.aspx 頁(yè)面。

3)Default.aspx 頁(yè)面

Default.aspx 頁(yè)面是主界面,打開主界面時(shí)會(huì)判斷 LoginSession 和 LoginCookie 的值是否正確,并把 Session 和 Cookie 的值顯示出來(lái)。

提交ASP.NET表單(即完成自動(dòng)登錄)的代碼如下:
try
???? {
????????CookieContainer cookieContainer = new CookieContainer();


????????
///
????????// 1. 打開 MyLogin.aspx 頁(yè)面,獲得 GetVeiwState & EventValidation
????????///????????????????
????????// 設(shè)置打開頁(yè)面的參數(shù)
????????string URI = "http://localhost:1165/WebTest/MyLogin.aspx";
???????? HttpWebRequest request
= WebRequest.Create(URI) as HttpWebRequest;
???????? request.Method
= "GET";
???????? request.KeepAlive
= false;

????????
// 接收返回的頁(yè)面
???????? HttpWebResponse response = request.GetResponse() as HttpWebResponse;
???????? System.IO.Stream responseStream
= response.GetResponseStream();
???????? System.IO.StreamReader reader
= new System.IO.StreamReader(responseStream,Encoding.UTF8);
????????
string srcString = reader.ReadToEnd();

????????
// 獲取頁(yè)面的 VeiwState????????????????
????????string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
????????int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
????????
int j = srcString.IndexOf("\"", i);
????????string viewState = srcString.Substring(i, j - i);

????????
// 獲取頁(yè)面的 EventValidation????????????????
????????string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
???????? i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
???????? j
= srcString.IndexOf("\"", i);
????????string eventValidation = srcString.Substring(i, j - i);
///
????????// 2. 自動(dòng)填充并提交 MyLogin.aspx 頁(yè)面
???????? ///
???????? // 提交按鈕的文本
???????? string submitButton = "登錄"; // 用戶名和密碼
???????? string userName = "1";
???????? string password = "1"; // 將文本轉(zhuǎn)換成 URL 編碼字符串
???????? viewState = System.Web.HttpUtility.UrlEncode(viewState);
???????? eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
???????? submitButton = System.Web.HttpUtility.UrlEncode(submitButton); // 要提交的字符串?dāng)?shù)據(jù)。格式形如:user=uesr1&password=123
???????? string formatString =
????????????????? "userName={0}&password={1}&loginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";
???????? string postString =
????????????????? string.Format(formatString, userName, password, submitButton, viewState, eventValidation); // 將提交的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)組
???????? byte[] postData = Encoding.ASCII.GetBytes(postString); // 設(shè)置提交的相關(guān)參數(shù)
???????? request = WebRequest.Create(URI) as HttpWebRequest;
???????? request.Method = "POST";
???????? request.KeepAlive = false;
???????? request.ContentType = "application/x-www-form-urlencoded";
???????? request.CookieContainer = cookieContainer;
???????? request.ContentLength = postData.Length; // 提交請(qǐng)求數(shù)據(jù)
???????? System.IO.Stream outputStream = request.GetRequestStream();
???????? outputStream.Write(postData, 0, postData.Length);
???????? outputStream.Close(); // 接收返回的頁(yè)面
???????? response = request.GetResponse() as HttpWebResponse;
???????? responseStream = response.GetResponseStream();
???????? reader = new System.IO.StreamReader(responseStream,Encoding.GetEncoding("GB2312"));
???????? srcString = reader.ReadToEnd();
???????? foreach (Cookie cookie in response.Cookies)
???????????? cookieContainer.Add(cookie); ///
???????? // 3. 打開 Default.aspx 頁(yè)面
???????? ///
???????? // 設(shè)置打開頁(yè)面的參數(shù)
???????? URI = "http://localhost:1165/WebTest/Default.aspx";
???????? request = WebRequest.Create(URI) as HttpWebRequest;
???????? request.Method = "GET";
???????? request.KeepAlive = false;
???????? request.CookieContainer = cookieContainer; // 接收返回的頁(yè)面
???????? response = request.GetResponse() as HttpWebResponse;
???????? responseStream = response.GetResponseStream();
???????? reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
???????? srcString = reader.ReadToEnd(); ///
???????? // 4. 分析返回的頁(yè)面
???????? ///
???????? //??
???? }
???? catch (WebException we)
???? {
???????? string msg = we.Message;
???? }??

之所以能夠保持 Session 和 Cookie 是因?yàn)槭褂昧?Cookie 容器(CookieContainer),見紅色的代碼部分。 本文所有源代碼:http://files.cnblogs.com/gotolnc/AutoPostWithCookies.rar

轉(zhuǎn)載于:https://www.cnblogs.com/kenter/archive/2009/11/10/1599612.html

總結(jié)

以上是生活随笔為你收集整理的C# 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。