Static variable Initialization

Source: Internet
Author: User

In order to record what network we are on, the company needs to use a proxy to change the network to access the Internet. You must enter your account and password before accessing the Internet.

In this way, the crawler code will be rewritten.

Uri target = new uri (Surl); httpwebrequest resquest = (httpwebrequest) webrequest. Create (target); proxysetting (resquest );...... // Set proxy Private Static void proxysetting (webrequest request) {WebProxy proxy = WebProxy. getdefaultproxy (); // obtain the default IE setting if (proxy. Address! = NULL) // if the address is null, no proxy server is required {try {string dm = system. configuration. configurationmanager. appsettings ["strdomain"]; string name = system. configuration. configurationmanager. appsettings ["strusername"]; string Pwd = system. configuration. configurationmanager. appsettings ["strpassword"]; proxy. credentials = new networkcredential (name, PWD, DM); Request. proxy = proxy;} catch (exception e) {Throw e ;}}}

In this case, you need to judge, read, and set the proxy for each capture. This is a great waste for a multi-threaded crawler program, resulting in a much longer crawling time.

Can I set this proxy at one time and assign it directly to this webrequest. Proxy every time I capture it? Or some network environments do not require a proxy at all, so you can not set this proxy?

The simplest way is to configure the proxy on the caller, and then pass the proxy as a parameter (the environment without proxy, proxy = NULL) to the webrequest directly. proxy. In this case, the function parameters change, causing many changes to use this function, resulting in a high cost. It is not intelligent to throw the setup work to the caller. What if I don't know how to set this proxy when I call it?

Another problem is that the crawling function is a static function, which means that we cannot construct an instance, initialize a proxy member variable first, and then call the crawling function.

Neither can this be done, nor can it be done.

In October, the revolution sounded like a gun, and later I thought of static variables. Using proxy as a static variable, it is estimated that it only needs to be initialized once (that is, at the time of the first call). In the future, there will be no need to initialize the process in the life cycle. It can be described as an initial and unlimited call.

// Static variable static WebProxy _ proxy = NULL; // static constructor static csocket () {WebProxy proxy = WebProxy. getdefaproxy proxy (); // obtain the default IE setting if (proxy. address! = NULL) {try {string dm = system. configuration. configurationmanager. appsettings ["strdomain"]; string name = system. configuration. configurationmanager. appsettings ["strusername"]; string Pwd = system. configuration. configurationmanager. appsettings ["strpassword"]; proxy. credentials = new networkcredential (name, PWD, DM); // create _ proxy = Proxy from the configuration encapsulation parameter;} catch (exception E) {}}// capture function URI target = new uri (Surl); httpwebrequest resquest = (httpwebrequest) webrequest. create (target); resquest. proxy = _ proxy;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.