`
hcmfys
  • 浏览: 347184 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java proxy 代理访问网络(转)

    博客分类:
  • java
 
阅读更多
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());
分享到:
评论

相关推荐

    JAVA 通过proxy代理方式访问internet资源

    JAVA 通过proxy代理方式访问internet资源,

    java使用proxy类设置代理ip

    获取网络资源,使用动态代理ip解决单个ip访问次数限制问题

    Java中使用IE Proxy代理的方法

    博客地址: https://blog.csdn.net/xcr530551426/article/details/95483176

    DotNet环境和Java环境的proxy配置文件

    内包含DotNet环境和Java环境的proxy配置文件,在ashx/jsp页面添加跳过SSL检测的代码,解决当arcgis server 网站证书不安全时,proxy代理访问出错问题

    socketproxy:一个简单的 Java 多线程 Socket 代理服务器。 它侦听传入的连接并将任何通信转发到服务器,同时记录整个对话

    套接字代理一个简单的 Java 多线程 Socket 代理服务器。 它侦听传入的连接并将任何通信转发到服务器,同时记录整个对话。套接字代理服务器这是库的主类。 它可以由第三方实例化,并提供本地端口和服务器的远程主机/...

    websocket-proxy:java websocket代理k8s docker终端

    websocket-proxy java websocket proxy k8s docker terminal 之所以有这个项目是因为有多个区域部署k8s集群, 每个集群里的容器都需要有终端访问。...proxy可以访问k8s-terminal,通过lb代理后的地址来访问。

    proxy-re-encryption-:基于JAVA,HTML,CSS和Java脚本的代理重新加密项目,用于通过加密和解密以及代理重新加密来安全存储数据

    代理重新加密基于JAVA,HTML,CSS和Java脚本的云存储设备项目中的代理重新加密,可使用加密和解密以及代理重新加密来安全地存储数据。 如今,IT部门的人们愿意将其数据存储到Cloud Storage中。 因为CSP(云服务提供...

    SocketHttp代理插件访问公网

    1、使用场景:应用部署的...2、附件有源码,直接打包运行SocketServer,就能启动一个Socket的Http访问代理服务了,Test源文件是调用例子,直接调用proxyRequest方法(传入ip,port,需要代理访问的公网url参数)即可。

    java 端口代理程序及源码

    假设你的主机为A,无法直接访问主机C,但可以通过主机B访问C,你可以在B上运行:java -jar proxy.jar host-c 1521 1521,通过这个命令,你可以通过访问B来访问C了。 又或者你在调试程序的时候,不知道中间传输了什么...

    java设计模式【之】JDK动态代理【源码】【场景:帮爸爸买菜】.rar

    * 动态代理中的静态方法:java.lang.reflect.Proxy.newProxyInstance (ClassLoader(类加载器),interface(接口),handler(监听处理器)) * * 代码示例:《帮爸爸买菜》 * 1.Father: 被代理类,必须需要实现接口 ...

    s3proxy:通过S3 API访问其他存储后端

    S3Proxy实现了和代理请求,从而启用了几种用例: 从S3到Backblaze B2,EMC Atmos,Google Cloud,Microsoft Azure和OpenStack Swift的转换使用本地文件系统在没有Amazon的情况下进行测试通过中间件扩展嵌入到Java...

    JAVA上百实例源码以及开源项目源代码

     Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...

    JAVA上百实例源码以及开源项目

     Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...

    编写简单的代理服务器(java源码)

    代理服务器打开一个端口接收浏览器发来的访问某个站点的请求,从请求的字符串中解析出用户想访问哪个网页,让后通过URL对象建立输入流读取相应的网页内容,最后按照web服务器的工作方式将网页内容发送给用户浏览器 ...

    java开源包4

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    HTTP-Proxy-Servlet:Smiley的HTTP代理实现为Java Servlet

    Smiley的HTTP代理Servlet ...我已经看到许多以博客形式在网络上以源代码形式发布的快速代理。 我发现这样的代理可以支持有限的HTTP子集(例如仅GET请求),或遭受其他实现问题,例如性能问题或URL转

    java开源包11

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包6

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包9

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

    java开源包101

    PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...

Global site tag (gtag.js) - Google Analytics