How to write a Java application that can access a Web server on the Internet via proxy

Source: Internet
Author: User
Tags file url ftp require requires ftp protocol

This tip will tell you how to write a Java application that can access a Web server on the Internet through a proxy. Adding proxy support In a Java application requires a few extra lines of code and does not rely on any security "vulnerabilities."

Almost all companies are concerned about protecting their internal networks from hackers and thieves. A common security measure is a complete disconnect from the Internet. If hackers cannot connect to any of your machines, they will not be able to enter your system illegally. The downside side effect of this strategy is that internal users cannot access external Internet servers, such as Yahoo or Javaworld. To address this problem, network administrators typically install "proxy servers." In fact, an agent is a service that is installed between the Internet and the intranet to manage connections between these two areas. Agents help reduce external threats to security while also allowing internal users access to Internet services. Although Java makes it no longer difficult to write Internet clients, they are useless if the client cannot pass the proxy. Fortunately, Java makes it no longer difficult to use proxy support-which is true if you know the secret tactic.

The secret to combining Java and agents is to activate specific system properties during the Java runtime. These attributes are not written to the official file, but are esoteric in Java programmers as part of the Java legend. To support proxies, Java applications need to specify not only the information of the agent itself, but also the user information for authentication. Before you start using the Internet Protocol, you need to add the following lines of code to your program:

System.getProperties().put("proxySet","true");
System.getProperties().put("proxyHost","myProxyMachineName");
System.getProperties().put("proxyPort","85");

The first line above informs Java that you want to connect through the proxy, the second line specifies the machine where the agent is located, and the third line specifies the port on which the agent listens. Some agents require users to enter a username and password before authorizing users to access the Internet. If you use a Web browser that is located within a firewall, you may have encountered this situation. Here's how to perform the certification:

URLConnectionconnection=url.openConnection();
Stringpassword="username:password";
StringencodedPassword=base64Encode(password);
connection.setRequestProperty("Proxy-Authorization",encodedPassword);

The idea of this code is that you must adjust the HTTP headers to emit user information. This is done by calling Setrequestproperty (). This method allows you to process the HTTP headers before making the request. HTTP requires the user name and password to be encoded using Base64. Fortunately, there is a set of common domain APIs that will encode your code (see the Reference Resources section).

As you can see, adding proxy support to a Java application does not require much work. With today's knowledge, do a little research (you must find out how your agent handles the protocols you are interested in and how you authenticate them), and you can implement the proxy with other protocols.

FTP Agent

Scottd.taylor This tip to handle the FTP protocol agent:

defaultProperties.put("ftpProxySet","true");
defaultProperties.put("ftpProxyHost","proxy-host-name");
defaultProperties.put("ftpProxyPort","85");

You can then use the FTP protocol to access the file URL by using the following code:

Urlurl=newurl ("Ftp://ftp.netscape.com/pub/navigator/3.04/windows/readme.txt");

If someone has an example of using another Internet Protocol agent, I'd like to see it.

Note: The code example (Example.java) has been tested only under JDK1.1.4.

Follow-up skills!

For developers who are still using JDK1.1.7 (with WebSphere3.0), setting ProxyHost and ProxyPort as System properties does not work, Conn.getinputstream () either returns a connection timeout, or the host path is not found. However, I resolved this problem using a URL constructor that accepts host and port parameters (using my proxy host and port):

Publicurl (Stringprotocol,stringhost,intport,stringfile).

Authentication with a username and password does not work. "Basic" should be placed at the beginning of the authentication string, for example:

StringencodedPassword=base64Encode(password);

Should be:

Stringencodedpassword= "Basic" +base64encode (password);

You do not have to use a separate program for 64-bit encoding. You can use the Sun.misc.BASE64Encoder () class. The following is the code to complete these two changes:

System.getProperties().put("proxySet","true");
System.getProperties().put("proxyHost",proxyHost);
System.getProperties().put("proxyPort",proxyPort);
StringauthString="userid:password";
Stringauth="Basic"+newsun.misc.BASE64Encoder
().encode(authString.getBytes());
URLurl=newURL("http://java.sun.com/");
URLConnectionconn=url.openConnection();
conn.setRequestProperty("Proxy-Authorization",auth);

Here's how to use the SOCKS4 Proxy server:

System.getProperty("socksProxySet",true);
System.getProperty("socksProxyHost",proxyHostName);
System.getProperty("socksProxyPort",proxyPort);
UsuallytheproxyPortforSocks4isport1080

You can then connect using SOCKS4.

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.