Use httpurlconnection in the proxy Network Environment

Source: Internet
Author: User
Tags file url ftp protocol

The company has closed the network and has to use a proxy to access the Internet.
I am debugging the interface program again, recording how to use httpurlconnection under the proxy in the Java environment

Properties prop = system. getproperties ();
// Set the address of the proxy server to be used for HTTP access
Prop. setproperty ("HTTP. proxyhost", "proxy server address ");
// Set the port for HTTP access to the proxy server to be used
Prop. setproperty ("HTTP. proxyport", "proxy server port ");
// Set the username of the proxy server to be used for HTTP access
Prop. setproperty ("HTTP. proxyuser", "User Name ");
// Set the password of the proxy server to be used for HTTP access
Prop. setproperty ("HTTP. proxypassword", "password ");

Write an application accessed by proxy in Java
This section describes how to write Java applications that can access web servers over the Internet through proxy. Adding a proxy to a Java application requires only a few additional lines of code and does not rely on any security "Vulnerability ".

 
Almost all companies are very concerned about protecting their internal networks to prevent hackers and hackers. A common security measure is to completely disconnect from the Internet. If hackers cannot connect to any of your servers
And they cannot access your system illegally. The negative side of this policy is that internal users cannot access external internet servers, such as Yahoo or javaworld. To solve this problem
The Network Administrator usually installs the "Proxy Server ". In fact, a proxy is a service installed between the Internet and the Intranet to manage connections between the two domains. Proxy helps reduce external security
And allow internal users to access Internet services. Although Java makes it no longer difficult to write Internet clients, it is useless if clients cannot use proxies. Fortunately, Java
This makes it no longer difficult to use Proxy Support-this is a fact if you know the secrets.
The secret to Combining JAVA and proxy is to activate specific system attributes during Java runtime. These attributes are not
Writing a formal file is only a part of Java's legends that are kept confidential by Java programmers. To support proxy, Java applications not only need to specify the proxy information, but also need to specify
User information of the certificate. Before you start using Internet protocol, you need to add the following lines of code to the program:

System. getproperties (). Put ("proxyset", "true ");
System. getproperties (). Put ("proxyhost", "myproxymachinename ");
System. getproperties (). Put ("proxyport", "85 ");

The first line above notifies Java that you want to connect through the proxy, the second line specifies the machine where the proxy is located, and the third line specifies the port on which the proxy listens. Some proxies require users to enter the user name and password before authorizing users to access the Internet. If you use a web browser within the firewall, you may have encountered this situation. The following describes how to perform authentication:

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 header to send user information. This is achieved by calling setrequestproperty. This method allows you to send a request
Previously processed HTTP headers. The user name and password must be encoded with base64 for HTTP. Fortunately, there is a set of public domain APIs that will code on your behalf (see the reference resources section ).

As you can see, adding Proxy Support to a Java application does not require much work. With your current knowledge, you can use other protocols to implement proxy. (You must find out how your proxy processes the protocols you are interested in and how to perform user authentication.

FTP Proxy

Scottd. Taylor puts forward this secret to handle FTP protocol Proxy:

Defaultproperties. Put ("ftpproxyset", "true ");
Defaultproperties. Put ("ftpproxyhost", "proxy-host-name ");
Defaultproperties. Put ("ftpproxyport", "85 ");

Next, you can use the following code to access the file URL using the "ftp" protocol:

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

If someone has an example of using another Internet Protocol proxy, I 'd like to take a look.

Note: The sample code (example. Java) has been tested only in jdk1.1.4.

Follow-up tips!

 
For developers who are still using jdk1.1.7 (with WebSphere), setting proxyhost and proxyport as system properties is not supported.
Use; Conn. getinputstream () or return connection timeout, or the host path cannot be found. However, I use the URL constructor that accepts the host and port parameters.
Solve this problem (use my proxy host and port ):

Publicurl (stringprotocol, stringhost, intport, stringfile ).

The user name and password authentication method does not work. "Basic" should be placed at the beginning of the authentication string; for example:

Stringencodedpassword = base64encode (password );

It should be:

Stringencodedpassword = "Basic" + base64encode (password );

You do not need to use a separate program for 64-bit encoding. You can use the sun. Misc. base64encoder () class. The following code is used after the two changes are completed:

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 );

The following describes how to use the socks4 Proxy Server:

System. getproperty ("socksproxyset", true );
System. getproperty ("socksproxyhost", proxyhostname );
System. getproperty ("socksproxyport", proxyport );
Usuallytheproxyportforsocks4isport1080

Next, you can connect with 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.