HttpRequest获取网站信息的程序示例

分类: asp.net   出处:iocblog整理  更新时间:2008-11-11   添加到收藏  

  问题:有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。 
   
  解决:使用asp.net中的httprequest和httpresponse来实现。 
   
  要点:
  
  1。 通过附加一个cookiecontainer到httprequest对象中,可以得到登录后返回的代表session id的cookie。 见login方法
  
  2。 将此cookie包含在一个cookiecontainer中并附加到另一个httprequest请求中,则可以实现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();
   (来源www.iocblog.net)
  // 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;
  } 
   
   

[1] [2] 下一页


Tag: HttpRequest



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。