A way to read a file in a streaming way, you can set the timeout time

Source: Internet
Author: User

Code

Using System;
Using System.Xml;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.Net;
Using System.IO;

Namespace Blogdata.nms_common
{
<summary>
Define access to the data flow interface
</summary>
public interface Irequestable
{
Stream GetStream (string requesturl);
}
<summary>
Summary description of Http
</summary>
public class Http:irequestable
{
<summary>
Sets the delay time for the request, in seconds
</summary>
private int mtimeout = 1;
<summary>
̉ exception error message
</summary>
public string errormessage;
public string errormessage
{
get {return this.errormessage;}
}
<summary>
Constructors
</summary>
Public Http (int timeOut)
{
This.mtimeout = timeOut * 1000;
}
<summary>
Hiding members with methods
</summary>
<param name= "TimeOut" ></param>
<returns></returns>
public static Http getintance (int timeOut)
{
return new Http (TimeOut);
}
<summary>
Overload uses method to hide members, defaults to 1 seconds
</summary>
public static Http Getintance ()
{
return new Http (1);
}

#region irequestface³éô±
<summary>
Implementing interfaces
</summary>
<param name= "Requesturl" ></param>
<returns></returns>
Public Stream GetStream (string requesturl)
{
Return GetStream (Requesturl,null);
}
<summary>
Overloaded interfaces
</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;
Judge if there is no http://head
String httphead = Requesturl.substring (0,7);
if (Httphead.tolower ()!= "http://") return null;
WebRequest WRQ = WebRequest.Create (Requesturl);
Wrq. Timeout = This.mtimeout; The requested time, in seconds
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)
{
Debug use, can also be converted to flow output, I did not convert
This.errormessage = We. message;
}
return null;
}
#endregion
}
}

Call Method:
Stream gets = Http.getintance (timeout). GetStream (URL);
Here you can judge the flow
if (gets = null)
Response.Write ("error");
Else
{
Because some domain name access is to return the Beijing broadband network or other error messages. Therefore, an exception cannot be caught, and the result is that the stream!=null the data by reading the stream.
Encoding encode =
System.Text.Encoding.GetEncoding ("gb2312");
Read stream
StreamReader reader = new StreamReader (gets, encode);
String str = reader. ReadToEnd (). ToString ();
Response.Write (str);
}
Note:
1, do not catch the exception here. If the return is: null, then an exception occurred. Because I'm in the class.
The exception has been handled.
2, the URL must be a full path
That is: with http://flag, otherwise cannot access



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.