Set HTTP access proxy server in C # code

Source: Internet
Author: User
When I started writing a blog reading tool, I always accessed it directly, and the access method was relatively simple:

Webrequest _ HWR = webrequest. createdefault (new system. Uri (URL ));


Webresponse _ hrs = _ HWR. getresponse ();


Stream receivestream = _ hrs. getresponsestream ();

But recently I opened the project and saw that the code was not running normally. After debugging, the client proxy was blocked by its, so I had to find the method for setting proxy access. The original code was very simple, you only need to add the following code.

WebProxy _ Wp = new WebProxy (proxyname, proxyport );

_ WP. bypassproxyonlocal = true;

Icredentials credentials = new networkcredential (username, userkey, domainname );

_ WP. Credentials = credentials;


Webrequest _ HWR = webrequest. createdefault (new system. Uri (URL ));

_ HWR. Proxy = _ WP;

Webresponse _ hrs = _ HWR. getresponse ();

Of course, on msdn, there is another way to use _ HWR. Proxy = _ WP;

Globalproxyselection. Select = _ WP; in fact, this is to set the global proxy service. You do not need to set them one by one. It is easier to capture page data.

Encoding encode = system. Text. encoding. default;

Streamreader sr = new streamreader (receivestream, encode );



String htmlcontent = "";



Char [] READ = new char [256];

Int COUNT = Sr. Read (read, 0,256 );

While (count> 0)

{

String STR = new string (read, 0, count );

Htmlcontent + = STR;

Count = Sr. Read (read, 0,256 );

}

OK. In this way, the code is configured as our proxy and the program is used to access the IE content ~~

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.