Scaff the source code of the specified URL from the Internet (C #)

xiaoxiao2021-03-06  124

Introduction: When doing wireless projects, some part of the communication company's data communication is interactive through XML, so we must dynamically capture data on the fixed Internet provided by the communication company, and study how to capture the fixed URL. Data, now share with you. Class name getPageCode, there is a method getSource, pass the attribute delivery parameters, the parameters are controlled to get the address of the URL, the proxy server settings and output methods, where everyone can expand their own needs, I only provide two The way, one is to write directly to a local file, and the other is to return a string. In the class, I have made more detailed notes. I think everyone is easy to understand. If I don't understand, I will ask MSN: Yubo@x263.net.

Calling method: #Region test Get remote page getPagecode GPC = new getPagecode (); gpc.url = "http://ppcode.com"; gpc.proxyState = 1; // Using proxy server, 0 is not used, set to 1 The proxy settings below act actively gpc.proxyaddress = "http://proxyname.com"; // proxy server address gpc.proxyport = "80"; // proxy server port gpc.proxyaccount = "proxy"; // Proxy server account gpc.proxypassword = "password"; // proxy server password gpc.proxyDomain = "bqc"; // proxy server domain gpc.outfilePath = filepath; // Setting the output file path, if not set, Return string gpc.getsource (); // Handling string temperr = gpc.noteMessage; // If an error, here will prompt string tempcode = gpc.outString; // Returns #ENDREGION Class code: use system; using System.collections; using system.data; using system.drawing; using system.io; using system.net; using system.text; using system.Web;

namespace Test.Com {///

/// function: Get the source URL of the page on the Internet /// Created: 2004-03-22 /// Author: Rexsp MSN: yubo@x263.net /// public class getPagecode {#Region private variable /// /// page URL address /// private string url = null; /// // Whether to use a code server : 0 Do not use 1 Using Proxy Server /// Private INT ProxyState = 0; /// /// proxy server address //////////////////////////////////>> Private string proxyAddress = NULL; /// < Summary> /// Proxy server port /// private string proxyport = null; /// /// proxy server username /// private string proxyAccount = null; /// /// proxy server password /// private string proxyPassword = null; /// /// proxy server domain /// prince proxydomain = null; /// /// Output file path /// private string outfilepath = null; /// /// output string /// private string outstring = null; // / /// Tip information /// private string NoteMessage; #ENDREGION

#Region Public Properties ///

/// To read URL Address ////////////////////////////bit URL;} set {url = value;}} /// // / Whether to use proxy server flag /// public int proxyState {get {return proxyState;} set {proxyState = value;}} /// /// proxy server address /// public string ProxyAddress {get {return proxyAddress;} set {proxyAddress = value;}} /// /// proxy port /// public string ProxyPort {get {return proxyPort;} set {proxyport = value;}} /// /// proxy server account /// public string proxyaccount {get {return proxyaccount;} set {proxyaccount = value;}} /// /// proxy server password /// public string proxypassword {get {return proxypassword;} set {proxypassword = value;}} /// /// proxy server domain /// Public string proxydomain {get {proxydomain = value;}} /// /// output file path /// public string outfilepath {get {return outfilepat H;} set {outfilepath = value;}} /// /// Returned String /// public string outstring {get {return outstring;}} /// // / Return prompt information /// public string NoteMessage {get {return noteMessage;}} #endregion #region constructor public getPagecode () {} #ENDREGION

#Region Public Method ///

/// Read Specify URL Address, Save //////////////////////bit () {WebRequest Request = WebRequest.create (this.URL); / / Using the proxy server processing if (this.ProxyState == 1) {// default read 80 port data 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 (); relatream.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 there is a file, you will do it out first (FI.EXISTS) fi.delete (); streamwriter sw = new streamwriter (this.outfilepath, true, eNCoding.default); SW. Write (Tempcode); sw.flush (); sw.close ();}} catch {this.noteMessage = "error, please check if the network is connected;";}

} #Endregion

}

转载请注明原文地址:https://www.9cbs.com/read-101954.html

New Post(0)