netutils.java
来自「MyUploader 是一款使用 http 协议(RFC 1867)用于上传文件」· Java 代码 · 共 165 行
JAVA
165 行
/*
* Copyright 2006-2007 JavaAtWork All rights reserved.
* Use is subject to license terms.
*/
package javaatwork.myuploader.net;
import java.net.PasswordAuthentication;
import java.net.Socket;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javaatwork.myuploader.utils.Logger;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.sun.java.browser.net.ProxyInfo;
import com.sun.java.browser.net.ProxyService;
/**
* Utility class for network connections.
*
* @author Johannes Postma
*/
public class NetUtils {
/**
* Returns the port number of an url. If the url contains no port number
* port 80 is returned.
*
* @param url The url.
* @return The port number.
*/
public int getPort(URL url) {
int port = url.getPort();
if (port == -1) {
if (url.getProtocol().equalsIgnoreCase("https")) {
return 443;
} else {
return 80;
}
} else {
return port;
}
}
/**
* Creates the Socket. The Socket could be a normal Socket or an SSLSocket.
*
* @param url The URL.
* @param passwordAuthentication The login data for the proxy server or null.
* @throws ProxyAuthenticatorException If the user can not be authenticated.
* @throws Exception If an error occurred.
*/
public Socket createSocket(URL url, PasswordAuthentication passwordAuthentication) throws ProxyAuthenticatorException, Exception {
Socket socket = null;
String proxy = detectProxyForUrl(url);
// HTTPS
if (url.getProtocol().equalsIgnoreCase("https")) {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
// DIRECT
if (proxy == null) {
SSLSocketFactory socketFactory = sc.getSocketFactory();
socket = socketFactory.createSocket(url.getHost(), this.getPort(url));
}
// PROXY
else {
String proxyHost = proxy.substring(0, proxy.indexOf(":"));
String proxyPort = proxy.substring(proxy.indexOf(":") + 1);
SSLTunnelSocketFactory socketFactory = new SSLTunnelSocketFactory(proxyHost, proxyPort, sc);
socket = socketFactory.createSocket(url.getHost(), this.getPort(url), passwordAuthentication);
}
// HTTP
} else {
// DIRECT
if (proxy == null) {
socket = new Socket(url.getHost(), this.getPort(url));
}
// PROXY
else {
String host = proxy.substring(0, proxy.indexOf(":"));
String port = proxy.substring(proxy.indexOf(":") + 1);
socket = new Socket(host, Integer.parseInt(port));
}
}
return socket;
}
/**
* Returns the proxy for the specified URL.
*
* @param url The URL.
* @return null if no proxy can be found otherwise host:port
*/
public String detectProxyForUrl(URL url) {
try {
ProxyInfo[] proxyInfo = ProxyService.getProxyInfo(url);
if (proxyInfo == null || proxyInfo.length == 0) {
Logger.log("NetUtils", "detectProxyForUrl()", "ProxyService reported NULL proxy (no proxy assumed)");
return null;
} else {
Logger.log("NetUtils", "detectProxyForUrl()", "ProxyService returned proxy " + proxyInfo[0].getHost() + ":" + proxyInfo[0].getPort() + ".");
String host = proxyInfo[0].getHost();
int port = proxyInfo[0].getPort();
return host + ":" + port;
}
} catch (Exception ee) {
Logger.log("NetUtils", "detectProxyForUrl()", "Could not detect proxy info." + ee.toString());
}
return null;
}
/**
* True if a proxy is used for the specified URL.
*
* @param url The URL.
* @return True if a proxy is used for the specified URL.
*/
public boolean useProxy(URL url) {
String proxy = detectProxyForUrl(url);
if (proxy != null) {
return true;
} else {
return false;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?