Introduction:
When working on a wireless project, some of the data communication with communication companies interacts with each other through XML. Therefore, we must dynamically capture the data on the fixed Internet provided by communication companies, I have studied how to capture data on a fixed URL and want to share it with you.
The class name getpagecode has a getsource method that transmits parameters through properties. The input parameter controls the URL address to be obtained, the proxy server's settings, and the control of the output method, you can expand your needs here. I only provide two methods here, one is to directly write to a local file, and the other is to return a string. Class has been made more detailed notes, I want you to easily understand, if you really do not understand, then ask on MSN, MSN: yubo@x263.net.
Call method:
# Region Test Remote webpage Retrieval
Getpagecode GPC = new getpagecode ();
GPC. url = "http://ppcode.com ";
GPC. proxystate = 1; // use the proxy server. If the value of 0 is not used, the following proxy settings take effect after the value is set to 1.
GPC. proxyaddress = "http://proxyName.com"; // proxy server address
GPC. proxyport = "80"; // 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 path of the output file. If not set, a string is returned.
GPC. getsource (); // Process
String temperr = GPC. notemessage; // if an error occurs, a message is displayed.
String tempcode = GPC. outstring; // returned string
# Endregion
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
{
/// <Summary>
/// Function: Obtain the source code of the URL page on the Internet
/// Creation:
/// By: rexsp MSN: yubo@x263.net
/// </Summary>
Public class getpagecode
{
# Region private variable
/// <Summary>
/// Webpage URL
/// </Summary>
Private string url = NULL;
/// <Summary>
/// Whether to use the code server: 0 does not use 1 uses the Proxy Server
/// </Summary>
Private int proxystate = 0;
/// <Summary>
/// Proxy server address
/// </Summary>
Private string proxyaddress = NULL;
/// <Summary>
/// Proxy server port
/// </Summary>
Private string proxyport = NULL;
/// <Summary>
/// Proxy Server Username
/// </Summary>
Private string proxyaccount = NULL;
/// <Summary>
/// Proxy server password
/// </Summary>
Private string proxypassword = NULL;
/// <Summary>
/// Proxy Server domain
/// </Summary>
Private string proxydomain = NULL;
/// <Summary>
/// Output file path
/// </Summary>
Private string outfilepath = NULL;
/// <Summary>
/// Output string
/// </Summary>
Private string outstring = NULL;
/// <Summary>
/// Prompt message
/// </Summary>
Private string notemessage;
# Endregion
# Region Public attributes
/// <Summary>
/// URL to be read
/// </Summary>
Public String URL
{
Get {return URL ;}
Set {url = value ;}
}
/// <Summary>
/// Whether to use the proxy server flag
/// </Summary>
Public int proxystate
{
Get {return proxystate ;}
Set {proxystate = value ;}
}
/// <Summary>
/// Proxy server address
/// </Summary>
Public String proxyaddress
{
Get {return proxyaddress ;}
Set {proxyaddress = value ;}
}
/// <Summary>
/// Proxy server port
/// </Summary>
Public String proxyport
{
Get {return proxyport ;}
Set {proxyport = value ;}
}
/// <Summary>
/// Proxy Server account
/// </Summary>
Public String proxyaccount
{
Get {return proxyaccount ;}
Set {proxyaccount = value ;}
}
/// <Summary>
/// Proxy server password
/// </Summary>
Public String proxypassword
{
Get {return proxypassword ;}
Set {proxypassword = value ;}
}
/// <Summary>
/// Proxy Server domain
/// </Summary>
Public String proxydomain
{
Get {return proxydomain ;}
Set {proxydomain = value ;}
}
/// <Summary>
/// Output file path
/// </Summary>
Public String outfilepath
{
Get {return outfilepath ;}
Set {outfilepath = value ;}
}
/// <Summary>
/// Returned string
/// </Summary>
Public String outstring
{
Get {return outstring ;}
}
/// <Summary>
/// Return the prompt message
/// </Summary>
Public String notemessage
{
Get {return notemessage ;}
}
# Endregion
# Region Constructor
Public getpagecode ()
{
}
# Endregion
# Region Public Method
/// <Summary>
/// Read the specified URL and save it to the specified file
/// </Summary>
Public void getsource ()
{
Webrequest request = webrequest. Create (this. url );
// Use the proxy server for processing
If (this. proxystate = 1)
{
// Read data from port 80 by default
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 ();
// Return information
Stream resstream = response. getresponsestream ();
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 attribute.
If (this. outfilepath = NULL)
{
This. outstring = tempcode;
}
Else
{
Fileinfo Fi = new fileinfo (this. outfilepath );
// If a file exists, kill it first.
If (Fi. exists)
Fi. Delete ();
Streamwriter Sw = new streamwriter (this. outfilepath, true, encoding. Default );
Sw. Write (tempcode );
Sw. Flush ();
Sw. Close ();
}
}
Catch
{
This. notemessage = "error occurred. Check whether the network is connected ;";
}
}
# Endregion
}
}