???????????
?????????
/**
* ??????????
*
* <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 :=================== " + 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());
}
}
????????????
???????????
????Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentication Required"
?????????????????δ???????????????£????????url????https???????????Authenticator???????????á?