Java使用CloseableHttpClient发送Http请求

使用CloseableHttpClient发送Http请求

使用org.apache.http.impl.client.CloseableHttpClient发送http请求,实现主动请求获取数据或者主动推送数据。

1. 使用Post方式推送Json数据,配置代理

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
public static String doPostSendJsonWithProxy(String url, String json) {
// 设置HttpHost代理
HttpHost proxy = new HttpHost("proxy_url", 8080, "http");

// 设置认证
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("proxy_username", "proxy_password"));

// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

// 设置config
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();

CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建HttpPost请求
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);

// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);

// 执行http请求
response = httpClient.execute(proxy, httpPost);

// 获得返回结果
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}

2. 使用Get方式获取数据,配置代理

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
public static String doGetFetchJsonWithProxy(String url){
// 设置HttpHost代理
HttpHost proxy = new HttpHost("proxy_url", 8080, "http");

// 设置认证
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("proxy_username", "proxy_password"));

// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();

// 设置config
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();

CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建HttpGet请求
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(config);

// 执行http请求
response = httpClient.execute(proxy, httpGet);

// 获得返回结果
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}

3. 发送Get请求

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
 public static String doGet(Map<String, String> header,String url, Map<String, String> param) {

// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();

String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);

if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();

// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
if (header!=null) {
Set<Entry<String,String>> set = header.entrySet();
for(Entry<String,String> en:set){
String key = en.getKey();
String value = en.getValue();
httpGet.addHeader(key, value);
}
}
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode()==200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}

public static String doGet(Map<String, String> header,String url) {
return doGet(header,url, null);
}

4. 发送Post请求

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
 public static String doPost(Map<String, String> header,String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if (header!=null) {
Set<Entry<String,String>> set = header.entrySet();
for(Entry<String,String> en:set){
String key = en.getKey();
String value = en.getValue();
httpPost.addHeader(key, value);
}
}
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return resultString;
}

public static String doPost(Map<String, String> header,String url) {
return doPost(header,url, null);
}

5. 发送Json数据

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
 public static String doPostJson(Map<String, String> header, String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if (header!=null) {
Set<Entry<String,String>> set = header.entrySet();
for(Entry<String,String> en:set){
String key = en.getKey();
String value = en.getValue();
httpPost.addHeader(key, value);
}
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return resultString;
}