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

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

  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
  
  
  
  }
  }(来源www.iocblog.net)

上一页 [1] [2]


Tag: HttpRequest



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