Use proxies and their validation (authentication) in Java HTTP connections (httpurlconnection) __java

Source: Internet
Author: User
Tags base64

Using Java's HttpURLConnection class enables you to implement HttpClient functionality without relying on any other class libraries. All of the times you use it directly to accomplish some simple (or complex) function. But you live behind the great {print G.F.W}, if you need to visit the site is a wall, then the HttpURLConnection class will have a connection timeout error. It is time to set up a proxy for him.

There are two ways to set up a proxy (proxy):

1, by setting the System Properties (System.setpropery (string key, String value) way

First you can see the properties supported by Java here. We can use the http.proxyhost,http.proxyport of these two properties. As the name suggests, is to set the proxy server address and proxy port respectively.

Set the attribute System.setproperty ("Http.proxyhost", "www.proxy.com") before you initiate the HTTP request; System.setproperty ("Http.proxyport", "8080");

Replace the above www.proxy.com for your proxy server address or IP address, and the corresponding port for the real port, HTTP connection and can work. It should be noted that if you set these properties, all HTTP requests will pass through the proxy server. These properties are JVM-level and are set to work for all of the same requests later. For example, the above is about HTTP, as well as about FTP and so on.

If your proxy server doesn't need to be validated, then it's over. However, it is generally necessary to verify. But if you look at the list of Java-supported properties above, you will find that there is no expected

Http.proxyusername=username Http.proxypassword=password these two properties. The Java.net.Authenticator class is required to complete the general HTTP validation. But the Java.net.Authenticator class is an abstract class, and we need to instantiate our own classes with an instantiation. Personally find it inconvenient here. As follows:

public class Basicauthenticator extends authenticator {String userName; String password; Public Basicauthenticator (String userName, string password) {this.username = userName; this.password = password;}/** * Called when password authorization is needed. Subclasses should * override the default implementation, which returns NULL. * @return The passwordauthentication collected from the ' * user, or null if none is provided. * * @Override protected passwordauthentication getpasswordauthentication () {Return to new Passwordauthentication (userName , Password.tochararray ()); } }

We need to overwrite the Getpasswordauthentication () method of the Java.net.Authenticator class and return a passwordauthentication instance. To make him work, you also need to set up

Authenticator.setdefault (New Basicauthenticator (userName, password));

This provides authentication based on HTTP basic, and then the agent that needs to be validated can be used smoothly.

2, through the Java.net.Proxy class.

This is the instantiation of a proxy class that provides proxy server information, such as ports and addresses.

Proxy proxy = new Proxy (Proxy.Type.HTTP, new Inetsocketaddress (host, port)); URLConnection conn = url.openconnection (proxy);

You use proxies by passing a proxy parameter when you open an HTTP connection. If you need to verify the information, we can add an HTTP header parameter to implement.

The format is as follows: "Proxy-authorization" = "Basic Base64.encode (User:password)" String Headerkey = "Proxy-authorization"; String headervalue = "Basic" + Base64.encode (user+ ":" +password); Conn.setrequestproperty (Headerkey, Headervalue); //..........

Base64.encode (User:password) refers to the use of BASE64 encoded values as part of a value after the user name and password are connected by a colon.

This way only affects a particular HTTP connection, but the code needs to be modified. Whether or not you can use authenticator in this way is not validated.

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.