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

xiaoxiao2021-03-06  109

Zyvslxl published on October 16, 2004

From:

Http://blog.blogchina.com/Article_49413.178037.html

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 data on the fixed URL. Now share it 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. More details have been made in the class

Thin notes, I think everyone can read it. If I don't understand, I will ask, MSN: Yubo@x263.net.

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

///

Public class getPagecode

{

#Region private variable

///

/ / / 网 page URL address

///

Private string URL = NULL;

///

// / Whether to use a code server: 0 Do not use 1 Proxy server ///

Private int proxyState = 0;

///

/// proxy server address

///

PRIVATE STRING ProxyAddress = NULL;

///

/// proxy server port

///

Private string proxyport = NULL;

///

/// Proxy server username

///

Private string proxyaccount = null;

///

/// proxy server password

///

Private string proxypassword = null;

///

/// proxy server domain

///

Private string proxydomain = NULL;

///

/// Output file path

///

Private string outfilepath = NULL;

///

/// Output string

///

Private string outstring = null;

///

// 提 提 信息

///

PRIVATE STRING NOTEMESSAGE;

#ndregion

#Region Public property

///

/// I want to read the URL address

///

Public String URL

{

Get {return URL;}

SET {URL = Value;

}

///

/ / / Whether to use a 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 server 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 {return proxydomain;

Set {proxydomain = value;

}

///

/// Output file path

///

Public String OutfilePath

{

Get {return outfilepath;}

Set {OutfilePath = value;

}

///

/// Returned string

///

Public String Outstring

{

Get {return outstring;}

}

///

/// Return prompt information

///

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

///

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

}

}

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

New Post(0)