日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET表單的Session和Cookie

使用HttpWebRequest提交ASP.NET表單并保持Session和Cookie

10年積累的成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有沙坡頭免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

由于種種原因,我們有時需要從互聯(lián)網(wǎng)上抓取一些資料,有些頁面可以直接打開,而有些頁面必登錄之后才能打開。本文介紹的是使用 HttpWebRequest 和 HttpWebResponse 自動填寫提交 ASP.NET表單并保持Session和Cookie 的一個完整的例子。本文所有源代碼:AutoPostWithCookies.rar

這里涉及到3個頁面:MyLogin.aspx,LoginOK.htm,Default.aspx:
1)MyLogin.aspx 頁面
2)LoginOK.htm 頁面
3)Default.aspx 頁面

提交ASP.NET表單(即完成自動登錄)的代碼如下:

 
 
 
  1. try  
  2. {  
  3. CookieContainercookieContainer=newCookieContainer();  
  4.  
  5. ///////////////////////////////////////////////////  
  6. //1.打開MyLogin.aspx頁面,獲得VeiwState&EventValidation  
  7. ///////////////////////////////////////////////////  
  8. //設(shè)置打開頁面的參數(shù)  
  9. stringURI="http://localhost:1165/WebTest/MyLogin.aspx";  
  10. HttpWebRequestrequest=WebRequest.Create(URI)asHttpWebRequest;  
  11. request.Method="GET";  
  12. request.KeepAlive=false;  
  13.  
  14. //接收返回的頁面  
  15. HttpWebResponseresponse=request.GetResponse()asHttpWebResponse;  
  16. System.IO.StreamresponseStream=response.GetResponseStream();  
  17. System.IO.StreamReaderreader=newSystem.IO.StreamReader(responseStream,Encoding.UTF8);  
  18. stringsrcString=reader.ReadToEnd();  
  19.  
  20. //獲取頁面的VeiwState  
  21. stringviewStateFlag="id=\"__VIEWSTATE\"value=\"";  
  22. inti=srcString.IndexOf(viewStateFlag)+viewStateFlag.Length;  
  23. intj=srcString.IndexOf("\"",i);  
  24. stringviewState=srcString.Substring(i,j-i);  
  25.  
  26. //獲取頁面的EventValidation  
  27. stringeventValidationFlag="id=\"__EVENTVALIDATION\"value=\"";  
  28. i=srcString.IndexOf(eventValidationFlag)+eventValidationFlag.Length;  
  29. j=srcString.IndexOf("\"",i);  
  30. stringeventValidation=srcString.Substring(i,j-i);  
  31.  
  32. ///////////////////////////////////////////////////  
  33. //2.自動填充并提交MyLogin.aspx頁面  
  34. ///////////////////////////////////////////////////  
  35. //提交按鈕的文本  
  36. stringsubmitButton="登錄";  
  37.  
  38. //用戶名和密碼  
  39. stringuserName="1";  
  40. stringpassword="1";  
  41.  
  42. //將文本轉(zhuǎn)換成URL編碼字符串  
  43. viewState=System.Web.HttpUtility.UrlEncode(viewState);  
  44. eventValidation=System.Web.HttpUtility.UrlEncode(eventValidation);  
  45. submitButton=System.Web.HttpUtility.UrlEncode(submitButton);  
  46.  
  47. //要提交的字符串?dāng)?shù)據(jù)。格式形如:user=uesr1&password=123 
  48. stringformatString=  
  49. "userName={0}&password={1}&loginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";  
  50. stringstringpostString=  
  51. string.Format(formatString,userName,password,submitButton,viewState,eventValidation);  
  52.  
  53. //將提交的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)組  
  54. byte[]postData=Encoding.ASCII.GetBytes(postString);  
  55.  
  56. //設(shè)置提交的相關(guān)參數(shù)  
  57. request=WebRequest.Create(URI)asHttpWebRequest;  
  58. request.Method="POST";  
  59. request.KeepAlive=false;  
  60. request.ContentType="application/x-www-form-urlencoded";  
  61. request.CookieContainer=cookieContainer;  
  62. request.ContentLength=postData.Length;  
  63.  
  64. //提交請求數(shù)據(jù)  
  65. System.IO.StreamoutputStream=request.GetRequestStream();  
  66. outputStream.Write(postData,0,postData.Length);  
  67. outputStream.Close();  
  68.  
  69. //接收返回的頁面  
  70. response=request.GetResponse()asHttpWebResponse;  
  71. responseresponseStream=response.GetResponseStream();  
  72. reader=newSystem.IO.StreamReader(responseStream,Encoding.GetEncoding("GB2312"));  
  73. srcString=reader.ReadToEnd();  
  74.  
  75. ///////////////////////////////////////////////////  
  76. //3.打開Default.aspx頁面  
  77. ///////////////////////////////////////////////////  
  78. //設(shè)置打開頁面的參數(shù)  
  79. URI="http://localhost:1165/WebTest/Default.aspx";  
  80. request=WebRequest.Create(URI)asHttpWebRequest;  
  81. request.Method="GET";  
  82. request.KeepAlive=false;  
  83. request.CookieContainer=cookieContainer;  
  84.  
  85. //接收返回的頁面  
  86. response=request.GetResponse()asHttpWebResponse;  
  87. responseresponseStream=response.GetResponseStream();  
  88. reader=newSystem.IO.StreamReader(responseStream,Encoding.UTF8);  
  89. srcString=reader.ReadToEnd();  
  90.  
  91. ///////////////////////////////////////////////////  
  92. //4.分析返回的頁面  
  93. ///////////////////////////////////////////////////  
  94. //  
  95. }  
  96. catch(WebExceptionwe)  
  97. {  
  98. stringmsg=we.Message;  

說明:
1) 之所以能夠保持 Session 和 Cookie 是因?yàn)槭褂昧?Cookie 容器(CookieContainer),見紅色的代碼部分。
2) POST ASP.NET頁面時,需要把 VeiwState 和 EventValidation 數(shù)據(jù)也一同 POST 過去。以上介紹ASP.NET表單并保持Session和Cookie

【編輯推薦】

  1. ASP.NET開發(fā)技巧之Theme功能淺析
  2. 詳解ASP.NET動態(tài)編譯
  3. Apache支持ASP.NET方法淺析
  4. 淺談ASP.NET服務(wù)器標(biāo)準(zhǔn)控件
  5. ASP.NET中SQL Server數(shù)據(jù)庫備份恢復(fù)淺析

網(wǎng)頁標(biāo)題:ASP.NET表單的Session和Cookie
當(dāng)前URL:http://www.5511xx.com/article/dpocchh.html