Use HTTP proxy in Java

Source: Internet
Author: User
ArticleDirectory
    • Proxy and username/password
    • Bypass a proxy

You have to set the following properties:

 
HTTP. proxyhost (default: <none>) HTTP. proxyport (default: 80 if HTTP. proxyhost specified) HTTP. nonproxyhosts (default: <none>)

Note: Proxyhost,ProxyportAre deprecated. You have to prefix them with "HTTP .".
Note:Those properties are supported ented here: http://java.sun.com/javase/6/docs/technotes/guides/net/properties.html.

You can set the required properties when starting the JVM for a Java application from the command line:

 
Java-dhttp. proxyhost = myproxyserver.com-dhttp. proxyport = 80 myjavaapp

Or in your source:

System. setproperty ("HTTP. proxyhost", "myproxyserver.com"); system. setproperty ("HTTP. proxyport", "80 ");

SinceIn Java 1.5You can also pass a java.net. Proxy instance to the openconnection () method:

 
// Proxy instance, proxy IP = 123.0.0.1 with port 8080 proxy = new proxy (proxy. type. HTTP, new inetsocketaddress ("123.0.0.1", 8080); Url url = new URL ("http://www.yahoo.com"); httpurlconnection UC = (httpurlconnection) URL. openconnection (proxy); UC. connect (); string page; stringbuffer TMP = new stringbuffer (); bufferedreader in = new bufferedreader (New inputstreamreader (UC. getinputstream (); While (( Line = in. Readline ())! = NULL) {page. append (LINE + "\ n");} system. Out. println (PAGE );

So you don't need to set system properties.

You can use the default proxy as defined by your networking settings.

System. setproperty ("java.net. usesystemproxies", "true ");List L = NULL; try {L = proxyselector. getdefault (). select (New uri ("http://www.yahoo.com");} catch (urisyntaxexception e) {e. printstacktrace ();} If (L! = NULL) {for (iterator iter = L. iterator (); ITER. hasnext () {java.net. proxy proxy = (java.net. proxy) ITER. next (); system. out. println ("proxy hostname:" + proxy. type (); inetsocketaddress ADDR = (inetsocketaddress) proxy. address (); If (ADDR = NULL) {system. out. println ("No proxy");} else {system. out. println ("proxy hostname:" + ADDR. gethostname (); system. out. println ("proxy port:" + ADDR. getport ());}}}

To bypass the proxy,

 
URL url = new URL ("http://internal.server.local/"); urlconnection conn = URL. openconnection (proxy. no_proxy );
Proxy and username/password

You might need to identify yourself to the proxy server.

One way is to use the HTTP property "proxy-Authorization" with a username: Password base64 encoded.

 
System. setproperty ("HTTP. proxyhost "," myproxyserver.com "); system. setproperty ("HTTP. proxyport "," 80 "); Url url = new URL (" http: // someserver/somepage "); urlconnection UC = URL. openconnection (); string encoded = new string (base64.base64encode (new string ("username: Password "). getbytes (); UC. setrequestproperty ("proxy-Authorization", "Basic" + encoded); UC. connect ();

Note: For a base64 function, see this how-.

The following example dumps the content of a URL but before we identify ourself to the proxy.

Import java.net. *; import Java. io. *; public class urlutils {public static void main (string s []) {urlutils. dump ("http://www.yahoo.com"); system. out. println ("****************"); urlutils. dump ("https://www.paypal.com"); system. out. println ("***************");} public static void dump (string urlname) {try {datainputstream di = NULL; fileoutputstream fo = NULL; byte [] B = new byte [1]; // proxy system. set Property ("HTTP. proxyhost "," proxy. mydomain. local "); system. setproperty ("HTTP. proxyport "," 80 "); Url u = new URL (urlname); httpurlconnection con = (httpurlconnection) U. openconnection (); // It's not the greatest idea to use a sun. misc. * class // sun stronugly advises not to use them since they can // change or go away in a future release so beware. // sun. misc. base64encoder encoder = new sun. MIS C. base64encoder (); string encodeduserpwd = encoder. encode ("mydomain \ myuser: mypassword ". getbytes (); con. setrequestproperty ("proxy-Authorization", "Basic" + encodeduserpwd); // proxy ---------- di = new datainputstream (con. getinputstream (); While (-1! = Di. read (B, 0, 1) {system. out. print (new string (B) ;}} catch (exception e) {e. printstacktrace ();}}}

With jdk1.2, The java.net. authenticator can be used to send the credentials when needed.

 
Public static void dump (string urlname) {try {datainputstream di = NULL; fileoutputstream fo = NULL; byte [] B = new byte [1]; // proxy system. setproperty ("HTTP. proxyhost "," proxy. mydomain. local "); system. setproperty ("HTTP. proxyport "," 80 "); authenticator. setdefault (New authenticator () {protected passwordauthentication getpasswordauthentication () {return New passwordauthentication ("mydomain \ Use Rname "," password ". tochararray () ;}}); Url u = new URL (urlname); httpurlconnection con = (httpurlconnection) U. openconnection (); DI = new datainputstream (con. getinputstream (); While (-1! = Di. Read (B, 0, 1) {system. Out. Print (new string (B) ;}} catch (exception e) {e. printstacktrace ();}}
Bypass a proxy

In Intranet environment, you may need to bypass the proxy server and go directly to the HTTP server.

TheHTTP. nonproxyhostsProperty indicates the hosts which shocould be connected too directly and not through the proxy server. the value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.

 
Java.exe-dhttp. nonproxyhosts = "* .mycompany.com | *. mycompany. Local | localhost" myclass
Related Article

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.