How to make your Java applications work across proxies and firewalls?
Introduction
Java provides a rich API for networking and I/O. The initial releases of the JDK did not contain native support for proxies and firewall authentications -- however, as of J2SDK1.4, Java also provides native support for proxies and authentication. In this article, I will cover three mechanisms for making your Java applications proxy-aware -- the first two are high-level, but slightly less powerful methods, and the last one is a slighly lower-level but more powerful method.
Configuring your Java Runtime Environment to use proxies
Irrespective of which method you choose to use, you will have to configure the JRE to use proxies. This is done by setting the following System properties:
// This is for HTTP Proxies
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "proxy.somedomain.org");
System.getProperties().put("proxyPort", "3128");
// This is for FTP Proxies
defaultProperties.put( "ftpProxySet", "true" );
defaultProperties.put( "ftpProxyHost", "proxy-host-name" );
defaultProperties.put( "ftpProxyPort", "85" );
// This is for SOCKS Proxies
System.getProperties().setProperty("socksProxySet", "true");
System.getProperties().setProperty("socksProxyHost", proxyServerName);
System.getProperties().setProperty("socksProxyPort", proxyServerPort);
Java 2 Native Support
Java 2 has a native authentication framework which can be used to provide authentication for a wide range of services in a platform and protocol independent mechanism. This is done by providing Authenticator objects.
import java.net.*;
import java.io.*;
public class TestProxy{
public static final String CRLF = "\r\n";
public TestProxy(){
try{
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "proxy.domain.com");
System.getProperties().put("proxyPort", "3128");
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
TestProxy tp = new TestProxy();
}
}
class MyAuthenticator extends Authenticator{
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("username", "password".toCharArray());
}
}
Using URLConnection API
Prior to Java 2, the SDK did not have native support for proxies. So, how does you go about making your old Java code proxy aware? Well, actually its not all that difficult. Basically, you have to play aorund with the HTTP headers yourself.
For an HTTP proxy, all HTTP requests should contain a Proxy-Authorization header ([??]) which stores the user name and password in a Base-64 encoding. You can use any base-64 encoder (and there are many available) -- for this example, I have used the encoder provided by Sun itself. However, it is not part of the standard JDK, so this code is not guaranteed to work. A simple implementation of a Base-64 encoder can be found here [??]
Having encoded the user name and password, you simply need to open a URLConnection, and set the request propert as shown below. Thats it, you're done!
String authString = "username" + ":" + "password";
String auth = "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes());
URL url = new URL("http://www.google.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Proxy-Authorization", auth);
Using raw Socket Headers
The above two methods are nice and convinient, however, they are quite restrictive. For instance, consider the case of HTTP proxies. Both the methods discussed above will work only with the HTTP GET/PUT/POST methods. These work for most conventional web applications. But now suppose that you want to write a Jabber client which works behind firewalls. To do that, you will need a socket object which would work accross the proxy -- because you would like to do arbitrary read/writes to the server.
Many HTTP proxies allow this functionality via the HTTP CONNECT method. Basically, the proxy opens a socket to the server on your behalf and returns a handle of the socket to you. Any further traffic on that socket will be forward AS-IS without any inspection/modification by the proxy. For doing this in Java, you need to supply HTTP headers manually over a raw TCP socket. Here is how to do this:
String authString = "username" + ":" + "password";
String auth = "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes());
Socket socket = new Socket("vsnlproxy.iitk.ac.in", 3128);
OutputStream out = socket.getOutputStream();
out.write(("CONNECT toc.oscar.aol.com:9898 HTTP/1.1" + CRLF + "Host: toc.oscar.aol.com:9898" + CRLF).getBytes());
out.write(("Proxy-Authorization: " + auth + CRLF).getBytes());
out.write(CRLF.getBytes());
- 浏览: 357648 次
- 性别:
- 来自: 广州
最新评论
-
tianshawang:
楼主你好偏心啊,把代码给谷歌不给这里
java 实现windows资源管理器 -
tianshawang:
其实我是也想要资源管理器的代码来的。。
java 实现windows资源管理器 -
hcmfys:
lonuery 写道open file 是获取要进行复制的文件 ...
JAVA 复制 带进度条 时间 百分比 -
qq657052171:
怎么没有代码呢?
java 实现windows资源管理器 -
IT女民工:
楼主,求资源管理器代码~~~~
java 实现windows资源管理器
相关推荐
JAVA 通过proxy代理方式访问internet资源,
Java ProxyServer是一个基于Java实现的代理服务器类,它在客户端和服务器之间起到了中继的作用,允许数据在两者间传输。代理服务器在计算机网络中的主要功能是提供代理服务,它可以隐藏客户端的真实身份,增加网络...
ArcGIS JS API 跨域配置是指在 JS 开发中遇到的访问本地服务和外网服务的问题,需要使用 Proxy 代理来解决跨域访问文件的问题。ArcGIS 的帮助中已经有了相关的介绍和使用配置。 一、使用代理配置 在 ArcGIS JS API...
在Java编程中,有时我们需要通过Internet Explorer(IE)的代理设置来访问网络资源,这主要适用于需要模拟用户网络环境或避开某些网络限制的情况。本文将详细介绍如何在Java中使用IE Proxy代理来实现这一功能。 ...
本文将详细介绍如何在Android的Webview中实现通过系统代理访问网络的功能。 首先,确保在AndroidManifest.xml文件中添加了必要的权限,这是让Webview能够正常访问网络的基础。添加以下两行代码: ```xml ...
在实际应用中,为了确保安全性和性能,需要正确配置代理服务,例如限制哪些URL可以通过代理访问,设置认证机制,以及处理可能出现的错误。同时,理解代理服务的工作原理,如如何处理请求和响应,以及如何在客户端和...
HTTP代理(httpProxy)是一种网络服务,它充当客户端与服务器之间的中介,允许客户端通过代理服务器来访问其他Web资源。在Java开发中,实现HTTP代理功能可以帮助我们处理各种网络请求,提高性能,以及实现数据抓取、...
获取网络资源,使用动态代理ip解决单个ip访问次数限制问题
在这个例子中,博主键盘太阳(keyboardsun)展示了如何通过Java的`java.lang.reflect.Proxy`类和`java.lang.reflect.InvocationHandler`接口来构建JDBC代理。 【标签】:“jdbc aop 代理 proxy” - JDBC:JDBC是...
标题中的“从房屋买卖看 java proxy 模式”暗示了我们将通过一个具体的场景来探讨 Java 中的代理(Proxy)模式。在软件设计中,代理模式是一种结构型设计模式,它为其他对象提供一种代理以控制对这个对象的访问。在...
5. `PageProxy.java`:这可能是一个页面代理类,用于处理与页面相关的操作,可能是为了添加额外的特性,如缓存、计数或者访问控制。 6. `Graphic.java`:这个类可能表示图形对象,可能与`ProxyDisplay`一起工作,...
虽然Java和JavaScript是两种不同的语言,但在JavaScript中也有类似的概念,比如JavaScript的`Proxy`对象,它也允许在访问对象属性时添加拦截逻辑,但这属于JavaScript的ES6特性,与Java的Proxy机制在用途上相似,但...
8. **安全性考虑**: 由于代理服务器可能会处理敏感的网络通信,因此需要关注安全性问题,例如防止中间人攻击、确保数据加密以及限制未经授权的访问。 总结来说,`java_proxy_end`项目是一个实现了HTTP和HTTPS代理...
Java 实现免费代理IP的获取方式 并动态实时校验是否有效,java文件项目内含有Jsoup的Jar包(Jsoup是加工过的,含请求),有2个主入口程序: 其一:用于请求代理IP,并立即校验是否是一个有效的代理IP,如果有效,...
Java版的代理(proxy)是Java编程中一个重要的概念,主要用在分布式系统或网络通信中,用于在客户端和服务器之间建立一个中间层,以实现负载均衡、安全控制、缓存、日志记录等多种功能。在Java中,代理通常通过Java...
在“netty proxy 代理例子”中,我们将会探讨如何利用 Netty 实现一个代理服务器,该代理服务器可以转发客户端的请求到目标服务器,并将响应回传给客户端。 首先,我们需要理解代理服务器的基本原理。代理服务器...
3. 安全控制:通过GoProxy-Android,开发者可以实现对网络访问的精细化控制,例如限制某些应用程序的网络访问,或者对特定URL进行加密传输,增强移动设备的安全性。 三、Android Studio开发实践 1. 引入库:在...
无论是通过代理模式控制对象的访问,还是利用Java动态代理实现特定时刻的行为增强,都是在不改变现有代码结构的前提下实现功能扩展的有效手段。这对于提高代码的可维护性和灵活性具有重要意义。
"Java动态代理语法Proxy类原理详解" Java动态代理语法Proxy类原理详解是Java语言中的一种非常重要的语法机制,它可以动态地生成代理对象,从而对目标对象进行增强和修改。本文将详细介绍Java动态代理语法Proxy类...
综上所述,Proxy代理模式是设计模式中的一个重要组成部分,它允许我们在不改变原始对象的情况下,通过代理对象实现对原始对象的控制和扩展。在Java中,通过Java的Proxy类和InvocationHandler接口,我们可以轻松地...