Java.net. url sets proxy server and custom Host header to bypass DNS resolution

Source: Internet
Author: User

In practice, due to office network restrictions, you must specify a proxy when connecting to the Internet,

However, when accessing servers with some virtual host domain names, there is no corresponding DNS resolution record. Therefore, it is very troublesome to manually configure the hosts file for ing.

According to the java.net. url API, we sorted out how to set the proxy server and customize the Host header to bypass DNS resolution:

The Code is as follows:

[Code]

Public static void main (string [] ARGs) throws ioexception {
/**
* The following method is set globally and is not recommended. System. setproperty ("proxyset", "true ");
* System. setproperty ("proxyhost", "proxy.lizongbo.com ");
* System. setproperty ("proxyport", "8080 ");
*/
String urlstr = "http://about.me/lizongbo ";
Urlstr = "http://about.me/lizongbo ";
Httpurlconnection httpconn = NULL;
Stringbuilder sb = new stringbuilder ();
URL url = new URL (urlstr );
String hostname = URL. gethost ();
// Customize the IP address in the URL according to the configuration
If (hostname. tolowercase (). endswith (". mqq. Im ")){
Url = new URL (urlstr. Replace (hostname, "127.0.0.1 "));
}
System. Out. println (URL );
Proxy proxy = new proxy (proxy. type. HTTP, new inetsocketaddress (
"Proxy.lizongbo.com", 8080 ));
// Only use HTTP proxy for the current connection
Httpconn = (httpurlconnection) URL. openconnection (proxy );
Httpconn. setrequestmethod ("get ");
// Manually set the Host header to support virtual hosts. In this way, you can avoid the trouble of manually configuring/etc/hosts to bypass DNS resolution, especially when the program runs on different servers, It is very practical.
Httpconn. setrequestproperty ("host", hostname );
Httpconn. setconnecttimeout (5000 );
Httpconn. Connect ();
Inputstream is = httpconn. getinputstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (is,
"UTF-8 "));
String line = NULL;
While (line = Br. Readline ())! = NULL ){
SB. append (Line). append ('\ n ');
}
// Close the connection
Httpconn. Disconnect ();
System. Out. println (SB );
}

[/Code]

Reprinted from: http://618119.com/archives/2010/12/23/202.html

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.