Call mode:
#REGION test Get a remote web page
GetPagecode GPC = New getPageCode ();
gpc.url = "http://ppcode.com/";
GPC.ProxyState = 1; // Using proxy servers, 0 is not used, set to 1 after 1 after the following proxy settings
GPC.ProxyAddress = "http://proxyname.com"; // proxy server address
gpc.proxyport = "80"; // The port of the proxy server
gpc.proxyaccount = "proxy"; // proxy server account
gpc.proxypassword = "password"; // proxy server password
gpc.proxydomain = "BQC"; // Proxy server domain
GPC.outfilePath = filepath; // Set the place where the output file path is set, if not set, return a string
gpc.getsource (); // Process
String Temperr = gpc.noteMessage; // If an error, here will prompt
String tempcode = gpc.outstring; // Returned string
#ndregion
Class code:
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.drawing;
Using system.io;
Using system.net;
Using system.text;
Using system.Web;
Namespace Test.com
{
///
/// Function: Source code for the URL page on the Internet
/// Create: 2004-03-22
/// Author: Rexsp MSN: yubo@x263.net
/// summary>
Public class getPagecode, PUBLIC CLASS
{
#Region private variable
///
/ / / 网 page URL address
/// summary>
Private string URL = NULL;
///
/ / Whether to use a code server: 0 Do not use 1 proxy server
/// summary>
Private int proxyState = 0;
///
/// proxy server address
/// summary>
PRIVATE STRING ProxyAddress = NULL;
///
/// proxy server port
/// summary>
Private string proxyport = NULL;
///
/// Proxy server username
/// summary>
Private string proxyaccount = null;
///
/// proxy server password
/// summary>
Private string proxypassword = null;
///
/// proxy server domain
/// summary>
Private string proxydomain = NULL;
///
/// Output file path
/// summary>
Private string outfilepath = NULL;
///
/// Output string
/// summary>
Private string outstring = null;
///
// 提 提 信息
/// summary>
PRIVATE STRING NOTEMESSAGE;
#ndregion
#Region Public property
///
/// I want to read the URL address
/// summary>
Public String URL
{
Get {return URL;}
SET {URL = Value;
}
///
/ / / Whether to use a proxy server flag
/// summary>
Public int proxyState
{
Get {return proxyState;}
Set {proxyState = value;
}
///
/// proxy server address
/// summary>
Public String ProxyAddress
{
Get {return proxyaddress;}
Set {proxyaddress = value;
}
///
/// proxy server port
/// summary>
Public String Proxyport
{
Get {return proxyport;}
Set {proxyport = value;}
}
///
/// proxy server account
/// summary>
Public String Proxyaccount
{
Get {return proxyaccount;}
Set {proxyaccount = value;
}
///
/// proxy server password
/// summary>
Public String ProxyPassword
{
Get {return proxypassword;}
Set {proxypassword = value;}
}
///
/// proxy server domain
/// summary>
Public String Proxydomain, PUBLIC STRING
{
Get {return proxydomain;
Set {proxydomain = value;
}
///
/// Output file path
/// summary>
Public String OutfilePath
{
Get {return outfilepath;}
Set {OutfilePath = value;
}
///
/// Returned string
/// summary>
Public String Outstring
{
Get {return outstring;}
}
///
/// Return prompt information
/// summary>
Public String NoteMessage
{
Get {return notemessage;}
}
#ndregion
#REGION constructor
Public getPagecode ()
{
}
#ndregion
#Region Public Method
///
/// read the specified URL address, save it to the specified file /// summary>
Public void getsource ()
{
WebRequest Request = WebRequest.create (this.URL);
// Use the proxy server
IF (this.ProxyState == 1)
{
/ / Default reading data of the 80 port
IF (this.proxyport == null)
this.Proxyport = "80";
WebProxy myproxy = new webproxy ();
MYPROXY = (WebProxy) Request.Proxy;
MyProxy.address = New Uri (this.ProxyAddress ":" this.Proxyport);
MyProxy.credentials = new networkcredential (this.proxyaccount, this.proxypassword, this.proxydomain);
Request.Proxy = myproxy;
}
Try
{
// Request service
WebResponse response = Request.getResponse ();
//returned messages
Stream relatream = response.getResponseth_;
StreamReader SR = New StreamReader (RESSTREAM, System.Text.Encoding.default);
String Tempcode = Sr.ReadToend ();
RESSTREAM.CLOSE ();
sr.close ();
// If the output file path is empty, the obtained content is assigned to the Outstring property.
IF (this.outfilepath == null)
{
THIS.outstring = Tempcode;
}
Else
{
FileInfo Fi = New FileInfo (this.outfilepath);
// If there is a file,
IF (FI.EXISTS)
Fi.delete ();
StreamWriter SW = New StreamWriter (this.outfilepath, true, eNCoding.default);
SW.WRITE (TEMPCODE);
Sw.flush ();
SW.CLOSE ();
}
}
Catch
{
This.noteMessage = "Out, please check if the network is connected;";
}
}
#ndregion
}
}