1。 通過附加一個cookiecontainer到httprequest對象中,可以得到登錄后返回的代表session id的cookie。 見login方法
  2。 將此cookie包含在一個cookiecontainer中并附加到另一個httprequest請求中,則可以實現(xiàn)session的還原。見getpage方法 
   
  源程序如下: 
   
  gethttpinfo.aspx: 
  <%@ page language="c#" codebehind="gethttpinfo.aspx.cs" autoeventwireup="false" inherits="pdftest.gethttpinfo" %> 
  <!doctype html public "-//w3c//dtd html 4.0 transitional//en" > 
  <html> 
  <head> 
  <title>webform1</title> 
  <meta content="microsoft visual studio 7.0" name="generator"> 
  <meta content="c#" name="code_language"> 
  <meta content="javascript" name="vs_defaultclientscript"> 
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema"> 
  </head> 
  <body> 
  <form id="form1" method="post" runat="server"> 
  </form> 
  </body> 
  </html> 
   
   
  gethttpinfo.aspx.cs: 
  using system; 
  using system.collections; 
  using system.componentmodel; 
  using system.data; 
  //using system.data.oledb; 
  using system.drawing; 
  using system.web; 
  using system.web.sessionstate; 
  using system.web.ui; 
  using system.web.ui.webcontrols; 
  using system.web.ui.htmlcontrols; 
  using system.net; 
  using system.io; 
  using system.text; 
  using system.text.regularexpressions; 
  using microsoft.data.odbc; 
   
  namespace pdftest 
  { 
  /// <summary> 
  /// summary description for webform1. 
  /// </summary> 
  public class gethttpinfo : system.web.ui.page 
  { 
  protected static string cookieheader; 
   
   
  private void page_load(object sender, system.eventargs e) 
  { 
  // put user code to initialize the page here 
   
  string strresult; 
   
  if (httpcontext.current.application["cookieheader"] != null) 
  { 
  cookieheader = (string)httpcontext.current.application["cookieheader"]; 
  } 
  else 
  { 
  //login into the website and keep the cookie for the session in the application variable 
  string strlogin = login("http://www.thesiteyouwanttovisit/theloginpage.asp", "action=&userid=&password=") ; 
  } 
   
   
  strresult = getpage("http://www.thesiteyouwanttovisit/theloginpage.asp", "action=&data=") ; 
   
   
  //write the result to htm file 
  filestream htmfile = new filestream("c:/save.htm", filemode.openorcreate); 
  streamwriter sw = new streamwriter(htmfile); 
  sw.write(strresult); 
  sw.close(); 
  htmfile.close(); 
   
  // output the result 
  response.write(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)); 
  httpcontext.current.application.lock(); 
  httpcontext.current.application["cookieheader"] = cookieheader; 
  httpcontext.current.application.unlock(); 
   
  stream receivestream = res.getresponsestream(); 
  encoding encode = system.text.encoding.getencoding("utf-8"); 
  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) 
  { 
  httpwebresponse res = null; 
  string strresult = ""; 
   
  try 
  { 
   
  httpwebrequest req = (httpwebrequest)webrequest.create(url); 
  req.method = "post"; 
  req.keepalive = true; 
  req.contenttype = "application/x-www-form-urlencoded"; 
  cookiecontainer cookiecon = new cookiecontainer(); 
  req.cookiecontainer = cookiecon; 
  req.cookiecontainer.setcookies(new uri(url),cookieheader); 
  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(); 
  stream receivestream = res.getresponsestream(); 
  encoding encode = system.text.encoding.getencoding("utf-8"); 
  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; 
  } 
   
  #region web form designer generated code 
  override protected void oninit(eventargs e) 
  { 
  // 
  // codegen: this call is required by the asp.net web form designer. 
  // 
  initializecomponent(); 
  base.oninit(e); 
  } 
   
  /// <summary> 
  /// required method for designer support - do not modify 
  /// the contents of this method with the code editor. 
  /// </summary> 
  private void initializecomponent() 
  { 
  this.load += new system.eventhandler(this.page_load); 
   
  } 
  #endregion    
  } 
  }
新聞熱點
疑難解答
圖片精選