首页 技术 正文
技术 2022年11月14日
0 收藏 416 点赞 2,775 浏览 2655 个字

1 前言

有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http、https、ftp、socks代理。比如在IE浏览器设置代理。

java中设置代理的两种方式

那我们在我们的java程序中使用代理呢,有如下两种方式。直接上代码.

2 采用设置系统属性

import java.net.Authenticator;import java.net.PasswordAuthentication;import java.util.Properties;public class ProxyDemo1 {public static void main(String[] args) {Properties prop = System.getProperties();// 设置http访问要使用的代理服务器的地址prop.setProperty("http.proxyHost", "183.45.78.31");// 设置http访问要使用的代理服务器的端口prop.setProperty("http.proxyPort", "8080");// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");// 设置安全访问使用的代理服务器地址与端口// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问prop.setProperty("https.proxyHost", "183.45.78.31");prop.setProperty("https.proxyPort", "443");// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机prop.setProperty("ftp.proxyHost", "183.45.78.31");prop.setProperty("ftp.proxyPort", "21");prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");// socks代理服务器的地址与端口prop.setProperty("socksProxyHost", "183.45.78.31");prop.setProperty("socksProxyPort", "1080");// 设置登陆到代理服务器的用户名和密码Authenticator.setDefault(new MyAuthenticator("userName", "Password"));}static class MyAuthenticator extends Authenticator {private String user = "";private String password = "";public MyAuthenticator(String user, String password) {this.user = user;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(user, password.toCharArray());}}}

 

3  使用Proxy

 

import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.Authenticator;import java.net.HttpURLConnection;import java.net.InetSocketAddress;import java.net.PasswordAuthentication;import java.net.Proxy;import java.net.URL;public class ProxyDemo2 {public static void main(String[] args) throws Exception {URL url = new URL("http://www.3lai8.com");// /创建代理服务器InetSocketAddress addr = new InetSocketAddress("192.168.0.254", 8080);// Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理Authenticator.setDefault(new MyAuthenticator("username", "password"));// 设置代理的用户和密码HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);// 设置代理访问InputStreamReader in = new InputStreamReader(connection.getInputStream());BufferedReader reader = new BufferedReader(in);while (true) {String s = reader.readLine();if (s != null) {System.out.println(s);}}}static class MyAuthenticator extends Authenticator {private String user = "";private String password = "";public MyAuthenticator(String user, String password) {this.user = user;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(user, password.toCharArray());}}}

4 总结

OK,就这么的简单,搞定,用第一种方式是一种全局的代理,用第种方式可以针对具体的哪一个使用代理。知道了这些我们就可以做我们想做的事情了哦!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,897