This article only under the needle ad agent in the Internet situation:
The code is as follows:
1. Define variables:
Define variable #region define variable
Copy Code code as follows:
private String Strfirewallip
... {
Get
... {
return system.configuration.configurationsettings.appsettings["Strfirewallip"];
}
}
private String Strfirewallport
... {
Get
... {
return system.configuration.configurationsettings.appsettings["Strfirewallport"];
}
}
private String Struid
... {
Get
... {
return system.configuration.configurationsettings.appsettings["Struid"];
}
}
private String Strpwd
... {
Get
... {
return system.configuration.configurationsettings.appsettings["Strpwd"];
}
}
private String Strdomain
... {
Get
... {
return system.configuration.configurationsettings.appsettings["Strdomain"];
}
}
#endregion
Method:
Get the specified Remote Web page content
Copy Code code as follows:
/**////<summary>
Get the specified Remote Web page content
</summary>
<param name= "strURL" > The remote page address to look for </param>
<returns></returns>
[WebMethod (Description =] gets the contents of the specified remote Web page. ")]
public string Getpagecontent (string strurl)
... {
String strresult = "";
This. Currenturl = strURL;
if (this. Currenturl.tolower (). StartsWith ("http://") ==false)
This. Currenturl = "http://" +this. Currenturl;
Try
... {
Contentbytes = Gethtmlbyte (Currenturl);
}
catch (Exception err)
... {
strresult = "Request Error:" + Err. message;
}
if (contentbytes==null)
... {
throw new Exception ("No return value obtained");
}
strresult = Getstringfrombytearray (Contentbytes,encoding.utf8);
return strresult;
}
Gets the specified remote page element byte array::
Gets the specified remote page element byte array #region Gets the specified remote page element byte array
Copy Code code as follows:
/**////<summary>
Gets the specified remote page element byte array
</summary>
<param name= "strURL" > The remote page address to look for </param>
<returns></returns>
Private byte[] Gethtmlbyte (string strurl)
... {
String strpara= (Strurl.indexof ("?") >=0?strurl.substring (Strurl.indexof ("?") +1): "");
System.Text.Encoding Encoding = new UTF8Encoding ();
byte[] byte1 = encoding. GetBytes (Strpara);
byte[] Bytereturn = new byte[10000000];
if (Strurl.trim (). ToLower (). StartsWith ("http://") ==false)
... {
strURL = "http://" +strurl;
}
HttpWebRequest myhttpwebrequest = (HttpWebRequest) webrequest.create (strURL);
Myhttpwebrequest.allowautoredirect = true;
Myhttpwebrequest.keepalive = true;
Myhttpwebrequest.useragent = "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.0. NET CLR 1.1.4322) ";
System.Net. WebProxy proxy = new WebProxy (strfirewallip+ ":" +strfirewallport,true);
Proxy= (WebProxy) System.Net.GlobalProxySelection.Select;
System.Net.NetworkCredential mycredential = new NetworkCredential (struid,strpwd,strdomain);
Proxy. Credentials =mycredential;
Myhttpwebrequest.proxy = Proxy;
HttpWebResponse myhttpwebresponse = (HttpWebResponse) myhttpwebrequest.getresponse ();
byte[] BRead = new byte[1024];
int lngcount = 1;
int totallen = 0;
Stream recweb = Myhttpwebresponse.getresponsestream ();
Lngcount = Recweb.read (bread,0,1024);
while (lngcount>0)
... {
Array.copy (Bread,0,bytereturn,totallen,lngcount);
Totallen + = Lngcount;
Lngcount = Recweb.read (bread,0,1024);
}
Recweb.close ();
byte[] bytegets = new Byte[totallen];
Array.copy (Bytereturn,0,bytegets,0,totallen);
Bytereturn = null;
BRead = null;
return bytegets;
}
#endregion
Converts the specified byte array to a string::
Converts the specified byte array to a string #region the specified byte array as a string
Copy Code code as follows:
/**////<summary>
Converts a specified byte array to a string
</summary>
<param name= "Byteget" > byte array byte[]</param>
<param name= "myencoding" > Coding mode </param>
<returns></returns>
private static string Getstringfrombytearray (byte[] byteget,encoding myencoding)
... {
int i,lngcount;
StringBuilder atemp = new StringBuilder (10000);
Lngcount = Byteget.length;
for (i = 0;i<lngcount;i+= 10000)
... {
Atemp.append (Myencoding.getstring (Byteget,i, (lngcount>=i+10000?10000:lngcount-i)));
}
if (I<=lngcount)
... {
Atemp.append (Myencoding.getstring (Byteget,i, (lngcount-i)));
}
return atemp.tostring ();
}
#endregion
Borrow this, wrote a Chinese weather Network forecast service! It's cool!