[Z] Set global proxy in C # programs

Source: Internet
Author: User

Https://www.cnblogs.com/Javi/p/7274268.html

1. The Proxy property of the HttpWebRequest class, as long as the property is set to use the agent, as follows:

1//Set up Agent

2 WebProxy WP = new WebProxy ("41.76.44.76", 3128);

3 ICredentials Jxcredt = new NetworkCredential ("Proxy_username", "Proxy_password");

4 WP. Credentials = Jxcredt;

5

6 HttpWebRequest webreq = Httpwebrequest.create (URI);

7 Webreq. Proxy = wp;//assigns the proxy to HttpWebRequest's proxy property

2. However, every HttpWebRequest in the program needs to be set up so that there is a simpler way to do it, such as setting up a proxy somewhere, and then using the proxy for all requests from the whole program.

The answer is: there is such a way.

===================================

But before doing this, let's take a look at some of the processing of C # 's WebRequest class (that is, Httewebrequest's parent class) before sending it:

When a new WebRequest instance is created (via the Create method), its Proxy property is automatically initialized.

And it also has a Defaultwebproxy property, when the user does not manually set the Proxy property,

Then WebRequest will use Defaultwebproxy as its proxy;

The defaultwebproxy is to read the project's app. config file for initialization;

When there is no app. config file, or if the proxy is not configured in app. Config,

Defaultwebproxy is going to read the proxy settings for Internet Explorer (IE)

============================================

Tips:

How do I view the current request Rquest use a proxy?

3. There are several ways to set up a global agent:

The first kind: The program does not do any processing, only set IE agent;

The second type: in the program's app. config file, make the relevant configuration, official documents, such as:

1 <?xml version= "1.0" encoding= "Utf-8"?>

2 <configuration>

3 <system.net>

4 <defaultProxy>

5 <proxy

6 proxyaddress= "http://192.168.1.10:3128"

7/>

8 </defaultProxy>

9 </system.net>

Ten </configuration>

When you use app. Config to set up the agent, if you want to modify the proxy at run time, you need to edit the app. config file

can refer to: C # Read and write config configuration file

Third: Re-assign the value to Defaultwebproxy in the code as follows:

1 WebProxy WP = new WebProxy ("41.76.44.76", 3128);

2 ICredentials Jxcredt = new NetworkCredential ("Proxy_username", "Proxy_password");//If there is a user name and password to set

3 WP. Credentials = Jxcredt;

4

5 Webrequest.defaultwebproxy = WP;

4. Summary:

The first is set directly on IE, more suitable for normal testing, because let the user to do this setting is not rational;

The second and the third are relatively simple, as long as the set up, all WebRequest of the whole program will use the proxy, but the second also need to edit the app. config file, so non-rigid requirements, the proposed use of the third, the way to set defaultwebproxy;

If you do not want to use a proxy, set Defaultwebproxy to NULL, so that the agent is not affected even if IE or app. config is set.

Agent prioritization: Manually set webrequest.proxy properties > Defaultwebproxy > App. config > IE proxy settings;

[Z] Set global proxy in C # programs

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.