首页 > 开发 > 综合 > 正文

一个以流方式读取文件的方式,可以设置超时时间

2024-07-21 02:26:10
字体:
来源:转载
供稿:网友

代码

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://标志,否则不能访问

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表