欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java通過url讀取文件內(nèi)容示例

 更新時間:2014年01月17日 10:00:07   作者:  
這篇文章主要介紹了java通過url讀取文件內(nèi)容示例,大家參考使用吧

復制代碼 代碼如下:

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>
 /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
 /// 此方法的內(nèi)容。
 ///  </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();
            }
        }

相關文章

  • Spring中@Configuration注解和@Component注解的區(qū)別詳解

    Spring中@Configuration注解和@Component注解的區(qū)別詳解

    這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過如下一個案例,在不分析源碼的情況下,小伙伴們先來直觀感受一下這兩個之間的區(qū)別,需要的朋友可以參考下
    2023-09-09
  • java日期時間格式化@JsonFormat與@DateTimeFormat的使用

    java日期時間格式化@JsonFormat與@DateTimeFormat的使用

    本文主要介紹了java日期時間格式化@JsonFormat與@DateTimeFormat的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Spring gateway + Oauth2實現(xiàn)單點登錄及詳細配置

    Spring gateway + Oauth2實現(xiàn)單點登錄及詳細配置

    gateway是基于 WebFlux的響應式編程框架,所以在使用securityConfig時采用的注解是@EnableWebFluxSecurity,接下來通過本文給大家介紹Spring gateway + Oauth2實現(xiàn)單點登錄及詳細配置,感興趣的朋友一起看看吧
    2021-09-09
  • Java設計模式中的建造者模式詳解

    Java設計模式中的建造者模式詳解

    這篇文章主要介紹了Java設計模式中的建造者模式詳解,建造者模式使我們?nèi)粘9ぷ髦斜容^常見的一種設計模式,和工廠模式一樣屬于創(chuàng)建型設計模式,用于解耦對象創(chuàng)建和對象使用的邏輯,需要的朋友可以參考下
    2023-12-12
  • mybatis映射內(nèi)部類的使用及注意事項說明

    mybatis映射內(nèi)部類的使用及注意事項說明

    這篇文章主要介紹了mybatis映射內(nèi)部類的使用及注意事項說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • springboot整合mybatis plus與druid詳情

    springboot整合mybatis plus與druid詳情

    這篇文章主要介紹了springboot整合mybatis plus與druid詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的下伙伴可以參考一下
    2022-09-09
  • Java使用GUI實現(xiàn)貪吃蛇游戲詳解

    Java使用GUI實現(xiàn)貪吃蛇游戲詳解

    小時候經(jīng)常在諾基亞上玩的一個小游戲-貪吃蛇,你還記得嗎?本篇帶你重溫一下把它實現(xiàn),做的比較簡單,但還是可以玩的.感興趣的朋友快來看看吧
    2022-05-05
  • Java并發(fā)之Semaphore工具類r的全面解析

    Java并發(fā)之Semaphore工具類r的全面解析

    Semaphore 是 java.util.concurrent中非常有用的并發(fā)編程工具類,它通常被用于限制對某個資源或資源池的并發(fā)訪問數(shù)量,下面我們就來深入了解一下Semaphore的具體使用吧
    2024-02-02
  • Java字節(jié)與字符流永久存儲json數(shù)據(jù)

    Java字節(jié)與字符流永久存儲json數(shù)據(jù)

    本篇文章給大家詳細講述了Java字節(jié)與字符流永久存儲json數(shù)據(jù)的方法,以及代碼分享,有興趣的參考學習下。
    2018-02-02
  • idea中項目文件目錄消失如何解決

    idea中項目文件目錄消失如何解決

    這篇文章主要介紹了idea中項目文件目錄消失的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論