开发技巧-Java通过HttpProxy实现穿越
需求描述
准备工作
设计分析
一、设置代理服务器地址端口
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", "" + proxyPort);
// 针对https也开启代理
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", "" + proxyPort);
// 初始化proxy对象
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // 创建连接
URL u = new URL(url);
URLConnection conn = u.openConnection(proxy);
二、实现用户密码校验
String headerKey = "Proxy-Authorization";
String encoded = new String(Base64.encodeBase64((new String(proxyUser + ":" + proxyPass).getBytes())));
String headerValue = "Basic " + encoded;
conn.setRequestProperty(headerKey, headerValue);
public static class MyAuthenticator extends Authenticator {
String userName;
String password;
public MyAuthenticator (String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* 当需要使用密码校验时自动触发
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
}
MyAuthenticator auth = new MyAuthenticator(proxyUser, proxyPass);
Authenticator.setDefault(auth);
实例代码
入口类
/**
* 网络代理测试
*
* <pre>
* 设置代理主机及端口:系统变量(https 需同步设置)
* 设置代理验证方式:全局代理对象
*
*
* https链接错误:
* Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
* 使用全局代理验证解决
*
* </pre>
*
* @author tzz
* @createDate 2015年7月23日
*
*/
public class ProxyTest {
private static String proxyHost = "xxx.xxxxx.com";
private static int proxyPort = 8080;
private static String proxyUser = "user";
private static String proxyPass = "pass";
public static void main(String[] args) {
String url = "https://www.google.com/";
String content = doProxy(url);
System.out.println("Result :===================\n " + content);
}
/**
* 通过系统变量方式实现代理
*
* @param url
* @return
*/
public static String doProxy(String url) {
// 设置系统变量 System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", "" + proxyPort);
// 针对https也开启代理
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", "" + proxyPort);
// 设置默认校验器
setDefaultAuthentication(); //开始请求
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
HttpsURLConnection httpsCon = (HttpsURLConnection) conn;
httpsCon.setFollowRedirects(true); String encoding = conn.getContentEncoding();
if (StringUtils.isEmpty(encoding)) {
encoding = "UTF-8";
}
InputStream is = conn.getInputStream();
String content = IOUtils.toString(is, encoding);
return content;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
} /**
* 设置全局校验器对象
*/
public static void setDefaultAuthentication() {
BasicAuthenticator auth = new BasicAuthenticator(proxyUser, proxyPass);
Authenticator.setDefault(auth);
}
}
校验器
/**
* 实现sun.net的代理验证
*
* @author tzz
* @createDate 2015年7月23日
*
*/
public static class BasicAuthenticator extends Authenticator {
String userName;
String password;
public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* Called when password authorization is needed. Subclasses should override the default implementation, which returns null.
*
* @return The PasswordAuthentication collected from the user, or null if none is provided.
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//System.out.println("DEBUG === use global authentication of password");
return new PasswordAuthentication(userName, password.toCharArray());
}
}
常见问题
开发技巧-Java通过HttpProxy实现穿越的更多相关文章
- Java 8的五大开发技巧
转载:http://geek.csdn.net/news/detail/94219 在Java 9发布之前,我们来分享一些Java 8开发技巧,本文翻译自JetBrains高级开发主管Trisha G ...
- WebApp开发技巧大全 看了就明白了
[转载]阅读原文 自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于WEB形式的应用程 序,运行在高端的移动终端设备).开发者们都 ...
- Android开发技巧——大图裁剪
本篇内容是接上篇<Android开发技巧--定制仿微信图片裁剪控件> 的,先简单介绍对上篇所封装的裁剪控件的使用,再详细说明如何使用它进行大图裁剪,包括对旋转图片的裁剪. 裁剪控件的简单使 ...
- Android开发技巧——使用PopupWindow实现弹出菜单
在本文当中,我将会与大家分享一个封装了PopupWindow实现弹出菜单的类,并说明它的实现与使用. 因对界面的需求,android原生的弹出菜单已不能满足我们的需求,自定义菜单成了我们的唯一选择,在 ...
- Android开发技巧——实现可复用的ActionSheet菜单
在上一篇<Android开发技巧--使用Dialog实现仿QQ的ActionSheet菜单>中,讲了这种菜单的实现过程,接下来将把它改成一个可复用的控件库. 本文原创,转载请注明出处: h ...
- Android开发技巧——自定义控件之使用style
Android开发技巧--自定义控件之使用style 回顾 在上一篇<Android开发技巧--自定义控件之自定义属性>中,我讲到了如何定义属性以及在自定义控件中获取这些属性的值,也提到了 ...
- Android开发技巧——自定义控件之自定义属性
Android开发技巧--自定义控件之自定义属性 掌握自定义控件是很重要的,因为通过自定义控件,能够:解决UI问题,优化布局性能,简化布局代码. 上一篇讲了如何通过xml把几个控件组织起来,并继承某个 ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- ES6 Javascript 实用开发技巧
ES6 实用开发技巧 定义变量/常量 ES6 中新增加了 let 和 const 两个命令,let 用于定义变量,const 用于定义常量 两个命令与原有的 var 命令所不同的地方在于,let, c ...
随机推荐
- 【转载】jQuery1.5之后的deferred对象详解
原文:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 原文作者 ...
- 【教程】Asset Server(联合开发)
Unity Asset Server下载 https://unity3d.com/cn/unity/team-license http://tieba.baidu.com/p/2419391804 W ...
- oracle PROCEDURE AS IS区别
在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别,在视图(VIEW)中只能用 ,在游标(CURSOR)中只能用IS不能用AS.
- C++ | boost库 类的序列化
是的,这是今年的情人节,一篇还在研究怎么用的文章,文结的时候应该就用成功了. 恩,要有信心 神奇的分割线 不知何时装过boost库的header-only库, 所以ratslam中的boost是可以编 ...
- Java—Applet
1 Applet的定义 Applet是Java语言编写的,无法独立运行,但可以嵌入到网页中执行.它扩展了传统的编程结构和方法,可以通过互联网发布到任何具有Java编译环境浏览器的个体计算机上. 如下 ...
- Maven引入本地jar包
<dependency> <groupId>${gorup}</groupId> <artifactId>${artifact}</artifac ...
- PHP数据库页面增删查
1.用户操作页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- linux下用phpize给PHP动态添加扩展
使用php的常见问题是编译php时忘记添加某扩展,后来想添加扩展,但是因为安装php后又装了一些东西如PEAR等,不想删除目录重装,这里就需要用到phpize了. 如我想增加bcmath扩展的支持,这 ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- Css中常用中文字体的Unicode编码对照
在网页制作中,最常用的恐怕是字体属性了,在调整页面兼容的时候,也常常发现字体名称的原因导致不兼容或乱码,下面给出几种常用字体的ucicode编码对照,方便使用. 宋体 SimSun \5B8B\4F5 ...