JAVA accesses external resources via proxy __java

Source: Internet
Author: User
Tags readline

Accessing external resources through proxies in code
The most popular protocol on the web today is the HTTP protocol. The HTTP protocol has many advantages, such as its ability to traverse firewalls. HTTP is also the basis for many other protocols, such as the SOAP protocol, which is built on the HTTP protocol.

Java provides support for HTTP through two APIs, the Servlet API, which overrides server-side programming issues, and the java.net package, which provides support for HTTP protocols through the HttpURLConnection class on the client. But I have encountered some problems when using the Servlet API. This article will introduce some of the problems I have encountered and the corresponding solutions.

Basic knowledge

Typically in a corporate network, a terminal is connected to the Internet through a proxy server, which is responsible for monitoring network traffic and enforcing security rules. In the Java.net API, the software can support proxy servers through two properties, which are Http.proxyhost and Http.proxyport respectively. They must be set to the appropriate proxy server and port, and the following code shows how to set these two properties:

String url = "http://www.digitalcq.com/",
Proxy = "Proxy.digitalcq.com",
Port = "8080";
URL server = new URL (URL);
Properties systemproperties = System.getproperties ();
Systemproperties.setproperty ("Http.proxyhost", proxy);
Systemproperties.setproperty ("Http.proxyport", port);
HttpURLConnection connection = (
httpurlconnection) server.openconnection ();
Connection.connect ();
InputStream in = Connection.getinputstream ();
Readresponse (in);


In the above program, you need to set the proxy server and port according to the actual situation. If you don't know how to set it up, you can ask your company's network administrator.

Using validation

Usually the company will require employees to log on to the proxy server before connecting to the Internet. This mechanism allows companies to better monitor the use of the Internet by their employees, such as monitoring which sites their employees have visited. HttpURLConnection supports proxy server authentication through the validation class. Here is an example of how to use the HttpURLConnection class for validation. First you need to implement a verifier:

public class Simpleauthenticator
Extends authenticator
{
Private String username,
Password
Public simpleauthenticator (String username,string password)
{
This.username = Username;
This.password = password;
}
Protected Passwordauthentication getpasswordauthentication ()
{
return new Passwordauthentication (
Username,password.tochararray ());
}
}


Then, register the authenticator through the Authenticator.setdefault () method:

String url = "http://www.digitalcq.com/",
Proxy = "Proxy.digitalcq.com",
Port = "8080",
Username = "USR",
Password = "PWD";
Authenticator.setdefault (New Simpleauthenticator (
Username,password));
URL server = new URL (URL);
Properties systemproperties = System.getproperties ();
Systemproperties.setproperty ("Http.proxyhost", proxy);
Systemproperties.setproperty ("Http.proxyport", port);
HttpURLConnection connection = (
httpurlconnection) server.openconnection ();
Connection.connect ();
InputStream in = Connection.getinputstream ();
Readresponse (in);


Problem

All of the above are described in the case where the HttpURLConnection class can work correctly. But I have encountered some network in the actual situation, in these networks, the HttpURLConnection class does not work correctly. Finally, I found that the key problem was using an incorrect DNS configuration. In practice, the HttpURLConnection class always tries to use the DNS server to resolve the address name first. Typically, this attempt fails, and the proxy server redirects the connection. However, some DNS servers return incorrect responses, causing the program to throw unknownhostexception exceptions. As a programmer, the system will not change the DNS server configuration for your program, so you need to find a way to solve the problem. My solution is to solve this problem by implementing the HTTP protocol myself. For example, a GET command can be implemented using the following code:

String url = "http://www.digitalcq.com/",
Proxy = "Proxy.digitalcq.com",
Port = "8080",
authentication = "Usr:pwd";
URL server = new URL (URL);
Socket socket = new socket (proxy,port);
Writer Writer = new OutputStreamWriter (Socket.getoutputstream (),
"Us-ascii");
Writer.write ("Get" + server.toexternalform () + "http/1.0/r/n");
Writer.write ("Host:" + server.gethost () + "/r/n");
Writer.write ("Proxy-authorization:basic"
+ New Sun.misc.BASE64Encoder (). Encode (
Authentication.getbytes ())
+ "/r/n/r/n");
Writer.flush ();
BufferedReader reader = new BufferedReader (New InputStreamReader (
Socket.getinputstream (), "us-ascii"));
String line = Reader.readline ();
if (line!= null && line.startswith ("http/"))
{
int sp = Line.indexof (');
String status = line.substring (sp + 1,SP + 4);
if (Status.equals ("200"))
{
while (Line.length ()!= 0)
line = Reader.readline ();
Readresponse (reader);
}
Else
throw new FileNotFoundException ("Host reports Error" +
status);
}
Else
throw new IOException ("Bad Protocol");
Reader.close ();
Writer.close ();
Socket.close ();


In the above code, you can note that the proxy server's username and password are in the form of:

Username:password,

and the program encodes them based on base 64. If you need to refer to the HTTP protocol, you can go to http://www.ietf.org/rfc/rfc2616.txt.  

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.