一個以流方式讀取檔案的方式,可以設定逾時時間

來源:互聯網
上載者:User

代碼

using System;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
 
namespace blogdata.nms_common
{
       ///<summary>
       ///定義獲得資料流介面
       ///</summary>
       public interface IRequestable
       {
              Stream GetStream(string requestUrl);
       }
       ///<summary>
       /// Http 的摘要說明
       ///</summary>
       public class Http : IRequestable
       {
              ///<summary>
              ///佈建要求的延時時間,以秒為單位
              ///</summary>
              private int mTimeOut = 1;
              ///<summary>
              /// ̉異常錯誤資訊
              ///</summary>
              public string errorMessage;
              public string ErrorMessage
              {
                     get {return this.errorMessage;}
              }
              ///<summary>
              ///建構函式
              ///</summary>
              public Http(int timeOut)
              {
                     this.mTimeOut = timeOut * 1000;   
              }
              ///<summary>
              ///用方法隱藏成員
              ///</summary>
              ///<param name="timeOut"></param>
              ///<returns></returns>
              public static Http GetIntance(int timeOut)
              {
                     return new Http(timeOut);
              }
              ///<summary>
              ///重載用方法隱藏成員,預設為1秒
              ///</summary>
              public static Http GetIntance()
              {
                     return new Http(1);
              }
 
              #region IRequestface ³ÉÔ±
              ///<summary>
              ///實現介面
              ///</summary>
              ///<param name="requestUrl"></param>
              ///<returns></returns>
              public Stream GetStream(string requestUrl)
              {
                     return GetStream(requestUrl,null);
              }
              ///<summary>
              ///重載介面
              ///</summary>
              ///<param name="requestUrl"></param>
              ///<param name="postData"></param>
              ///<returns></returns>
              public Stream GetStream(string requestUrl,byte[] postData)
              {
                     if (requestUrl == null || requestUrl.Length == 0) return null;
                     //判斷有沒有http://頭
                     string httpHead = requestUrl.Substring(0,7);
                     if (httpHead.ToLower() != "http://") return null;
                     WebRequest wrq = WebRequest.Create(requestUrl);
                     wrq.Timeout = this.mTimeOut; //請求的時間,以秒為單位
                            if (postData != null)
                            {
                                   wrq.Method = "POST";
                                   wrq.ContentType =
                                          "application/x-www-form-urlencoded";
                                   wrq.ContentLength = postData.Length;
                                   Stream rs = wrq.GetRequestStream();
                                   rs.Write(postData,0,postData.Length);
                                   rs.Close();
                                   rs = null;
                            }
                     try
                     {
                            WebResponse wrp = wrq.GetResponse();
                            Stream res = wrp.GetResponseStream();
                            if(res != null) return res;
                     }
                     catch (WebException we)
                     {
                            //調試使用,也可以轉化為流輸出,我沒有轉化
                            this.errorMessage = we.Message;
                     }
                     return null;
              }
              #endregion
       }
}

調用方法:
Stream gets = Http.GetIntance(timeout).GetStream(url);
//這裡可以判斷一下流
if (gets == null)
          Response.Write("error");
else
{
          //由於有些網域名稱的訪問是返回了北京寬頻網或者其他的錯誤資訊。因此不能捕獲異常,結果為:流 !=null 通過讀出流看一下資料,就明白了。
         Encoding encode =
         System.Text.Encoding.GetEncoding("gb2312");
         //讀流
         StreamReader reader = new StreamReader(gets, encode);
         string str = reader.ReadToEnd().ToString();
         Response.Write(str);
}
註:
1、這裡就不要捕獲異常了。如果返回的為:null則就是發生了異常。因為我在類裡面
已經處理異常了。
2、Url必須為完整的路徑
即:帶有http://標誌,否則不能訪問



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。