添加依赖

1
2
3
4
5
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>

使用

1
private HttpClient httpClient = new HttpClient();

HttpClient 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* GET 请求 Https
*
* @param data
* @param url
* @return
*/
public String visitGetHttps(NameValuePair[] data, String url, String referer) {
try {

Protocol myHttps = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myHttps);

if ("true".equals(config.getIsUseProxy())) {
httpClient.getHostConfiguration().setProxy(config.getProxyHost(), Integer.parseInt(config.getProxyPort()));
System.out.println(" 使用代理:" + config.getProxyHost() + ":" + config.getProxyPort());
}

GetMethod getMethod = new GetMethod(url);
if (null != data) {
getMethod.setQueryString(data);
}
getMethod.setRequestHeader("Referer", referer);
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
int status = httpClient.executeMethod(getMethod);

return status == HttpStatus.SC_OK ? getMethod.getResponseBodyAsString() : null;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* POST 请求
*
* @param data
* @param url
* @return
*/
public String visitPostHttps(NameValuePair[] data, String url) {
try {
Protocol myHttps = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myHttps);

PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

postMethod.setRequestBody(data);
int status = httpClient.executeMethod(postMethod);
System.out.println("status: " + status);

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* GET HTTP
* @param data
* @param url
* @return
*/
public String visitGetHttp(NameValuePair[] data, String url) {
try {

if ("true".equals(config.getIsUseProxy())) {
httpClient.getHostConfiguration().setProxy(config.getProxyHost(), Integer.parseInt(config.getProxyPort()));
System.out.println(" 使用代理:" + config.getProxyHost() + ":" + config.getProxyPort());
}

GetMethod getMethod = new GetMethod(url);
if (null != data) {
getMethod.setQueryString(data);
}
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK");
int status = httpClient.executeMethod(getMethod);
return status == HttpStatus.SC_OK ? getMethod.getResponseBodyAsString() : null;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

忽略 SSL 证书工具类

使用方法

1
2
3
// 解决 HTTP --> HTTPS
Protocol myHttps = new Protocol("https", new MySecureProtocolSocketFactory(), 443);
Protocol.registerProtocol("https", myHttps);

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
* Created by a on 2017/8/1.
* 忽略 SSL 证书
* @author a
*/
public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
private SSLContext sslcontext = null;

private SSLContext createSSLContext() {
SSLContext sslcontext = null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}

private SSLContext getSSLContext() {
if (this.sslcontext == null) {
this.sslcontext = createSSLContext();
}
return this.sslcontext;
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
socket,
host,
port,
autoClose
);
}

@Override
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
host,
port
);
}

@Override
public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
}

@Override
public Socket createSocket(String host, int port, InetAddress localAddress,
int localPort, HttpConnectionParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {
if (params == null) {
throw new IllegalArgumentException("Parameters may not be null");
}
int timeout = params.getConnectionTimeout();
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (timeout == 0) {
return socketfactory.createSocket(host, port, localAddress, localPort);
} else {
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
SocketAddress remoteaddr = new InetSocketAddress(host, port);
socket.bind(localaddr);
socket.connect(remoteaddr, timeout);
return socket;
}
}

/**
* 自定义私有类
*/
private static class TrustAnyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
}