欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java通过url读取文件内容示例

程序员文章站 2024-02-22 14:42:04
复制代码 代码如下:using system;  using system.collections;  using system.componentmo...

复制代码 代码如下:

using system;
 using system.collections;
 using system.componentmodel;
 using system.data;
 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.io;
 using system.net;
 using system.text;
 using system.text.regularexpressions;
 namespace emeng.exam
 {
 ///  <summary>
 /// getpagehtml 的摘要说明。
 ///  </summary>
 public class getpagehtml : system.web.ui.page
 {
 protected system.web.ui.webcontrols.button webclientbutton;
 protected system.web.ui.webcontrols.button webrequestbutton;
 protected system.web.ui.webcontrols.textbox contenthtml;
 protected system.web.ui.webcontrols.textbox urltext;
 protected system.web.ui.webcontrols.button gettext;
 private string pageurl = "";

 private void page_load(object sender, system.eventargs e)
  {}

 #region web form designer generated code
 override protected void oninit(eventargs e)
  {
  initializecomponent();
  base.oninit(e);
 }

 ///  <summary>
 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 /// 此方法的内容。
 ///  </summary>
 private void initializecomponent()
 {    
     this.webclientbutton.click += new system.eventhandler(this.webclientbutton_click);
     this.webrequestbutton.click += new system.eventhandler(this.webrequestbutton_click);
     this.gettext.click += new system.eventhandler(this.gettext_click);
     this.load += new system.eventhandler(this.page_load);

 }
 #endregion

 private void webclientbutton_click(object sender, system.eventargs e)
 {
  pageurl = urltext.text;
  webclient wc = new webclient();
  wc.credentials = credentialcache.defaultcredentials;

  ///方法一:
  byte[] pagedata = wc.downloaddata(pageurl);
  contenthtml.text = encoding.default.getstring(pagedata);    

 
  /// 方法二:
  /// ***************代码开始**********
  /// stream resstream = wc.openread(pageurl);
  /// streamreader sr = new streamreader(resstream,system.text.encoding.default);
  /// contenthtml.text = sr.readtoend();
  /// resstream.close();
  /// **************代码结束********
  /// 
 wc.dispose();  
 }

 private void webrequestbutton_click(object sender, system.eventargs e)
 {
  pageurl = urltext.text;
  webrequest  request = webrequest.create(pageurl);
  webresponse response = request.getresponse();
  stream resstream = response.getresponsestream();    
  streamreader sr = new streamreader(resstream, system.text.encoding.default);
  contenthtml.text = sr.readtoend();
  resstream.close(); 
  sr.close();
 }

 private void gettext_click(object sender, system.eventargs e)
  {
     pageurl = urltext.text;
     webrequest  request = webrequest.create(pageurl);
     webresponse response = request.getresponse();
     stream resstream = response.getresponsestream();    
     streamreader sr = new streamreader(resstream, system.text.encoding.default);
     contenthtml.text = sr.readtoend();
     resstream.close(); 
     sr.close();
     contenthtml.text = regex.replace(contenthtml.text," <[^>]*>", "");
     //替换空格
     contenthtml.text = regex.replace(contenthtml.text,"\\s+", " ");
  }
 }

复制代码 代码如下:

private void toolstripbutton1_click(object sender, eventargs e)
        {
            string path = @"http://www.abc.com/0211155400.xml";
            httpwebrequest req = (httpwebrequest)webrequest.create(path);
            req.timeout = 10000;
            httpwebresponse rep = (httpwebresponse)req.getresponse();
            using (streamreader myfile = new streamreader(rep.getresponsestream()))
            {
                string mystring = myfile.readtoend();
            }
        }