自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET表單的Session和Cookie

開發(fā) 后端
本文介紹ASP.NET表單的Session和Cookie,使用 HttpWebRequest 和 HttpWebResponse 自動(dòng)填寫提交的一個(gè)完整的例子。

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

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

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

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

  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.自動(dòng)填充并提交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頁面時(shí),需要把 VeiwState 和 EventValidation 數(shù)據(jù)也一同 POST 過去。以上介紹ASP.NET表單并保持Session和Cookie

【編輯推薦】

  1. ASP.NET開發(fā)技巧之Theme功能淺析
  2. 詳解ASP.NET動(dòng)態(tài)編譯
  3. Apache支持ASP.NET方法淺析
  4. 淺談ASP.NET服務(wù)器標(biāo)準(zhǔn)控件
  5. ASP.NET中SQL Server數(shù)據(jù)庫備份恢復(fù)淺析
責(zé)任編輯:佚名 來源: MSDN
相關(guān)推薦

2009-07-29 17:23:17

ASP.NET表單

2009-07-29 10:19:48

Session StaASP.NET

2009-07-28 16:57:50

ASP.NET Ses

2009-07-21 11:11:44

刪除CookieASP.NET

2009-08-05 18:22:55

2009-07-24 10:41:00

ASP.NET Ses

2009-07-20 17:21:43

Session狀態(tài)ASP.NET

2009-07-23 18:56:50

ASP.NET Ses

2009-08-03 10:07:20

ASP.NET Ses

2009-08-06 15:56:40

ASP.NET Coo

2009-07-24 10:41:00

ASP.NET Ses

2009-07-22 18:02:26

ASP.NET Ses

2009-07-24 17:04:57

ASP.NET中Coo

2009-07-27 10:22:16

ASP.NET中Coo

2009-07-30 13:33:55

ASP.NET中的co

2009-07-27 12:22:03

ASP.NET和ASPASP.NET入門教程

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-30 14:03:04

ASP.NET中的se

2009-08-03 09:45:14

ASP.NET Ses

2009-07-23 16:28:55

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號