服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java通过url读取文件内容示例

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

2019-11-01 14:07java教程网 JAVA教程

这篇文章主要介绍了java通过url读取文件内容示例,大家参考使用吧

代码如下:


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();
            }
        }

延伸 · 阅读

精彩推荐