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