版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。部落格源位址為zhixiang.org.cn https://blog.csdn.net/myFirstCN/article/details/80880234
使用之前首先添加maven依賴或者是jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
下面給出工具類代碼,一個簡單發送Get和Post請求的例子
public class HttpUtils {
/*
*發送簡單post請求
*/
public static JSONObject post(String url) {
HttpPost post = new HttpPost(url);
return getResult(post);
}
/*
*發送帶Header的post請求
*/
public static JSONObject post(String url, Map<String, String> map) {
HttpPost post = new HttpPost(url);
if (!map.isEmpty()) {
Set<Map.Entry<String, String>> entrys = map.entrySet();
for (Map.Entry<String, String> entry : entrys) {
post.setHeader(entry.getKey(), entry.getValue());
}
}
return getResult(post);
}
/*
*發送帶Header的get請求
*/
public static JSONObject get(String url, Map<String, String> map) {
HttpGet get = new HttpGet(url);
if (!map.isEmpty()) {
Set<Map.Entry<String, String>> entrys = map.entrySet();
for (Map.Entry<String, String> entry : entrys) {
get.setHeader(entry.getKey(), entry.getValue());
}
}
return getResult(get);
}
/*
*發送簡單的get請求
*/
public static JSONObject get(String url) {
HttpGet get = new HttpGet(url);
return getResult(get);
}
/*
*發送請求方法,請求響應為JSONObject
*/
private static JSONObject getResult(HttpRequestBase requestBase) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
try {
result = EntityUtils.toString(httpClient.execute(requestBase).getEntity());
httpClient.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
return new JSONObject(JSON.parseObject(result));
}
}
/*
*當請求響應為String時
*/
public static String getString(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
String result = null;
try {
result = EntityUtils.toString(httpClient.execute(get).getEntity());
httpClient.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
return result;
}
}
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
public class HttpUtils {
/*
*發送簡單post請求
*/
public static JSONObject post(String url) {
HttpPost post = new HttpPost(url);
return getResult(post);
}
/*
*發送帶Header的post請求
*/
public static JSONObject post(String url, Map<String, String> map) {
HttpPost post = new HttpPost(url);
if (!map.isEmpty()) {
Set<Map.Entry<String, String>> entrys = map.entrySet();
for (Map.Entry<String, String> entry : entrys) {
post.setHeader(entry.getKey(), entry.getValue());
}
}
return getResult(post);
}
/*
*發送帶Header的get請求
*/
public static JSONObject get(String url, Map<String, String> map) {
HttpGet get = new HttpGet(url);
if (!map.isEmpty()) {
Set<Map.Entry<String, String>> entrys = map.entrySet();
for (Map.Entry<String, String> entry : entrys) {
get.setHeader(entry.getKey(), entry.getValue());
}
}
return getResult(get);
}
/*
*發送簡單的get請求
*/
public static JSONObject get(String url) {
HttpGet get = new HttpGet(url);
return getResult(get);
}
/*
*發送請求方法,請求響應為JSONObject
*/
private static JSONObject getResult(HttpRequestBase requestBase) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = null;
try {
result = EntityUtils.toString(httpClient.execute(requestBase).getEntity());
httpClient.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
return new JSONObject(JSON.parseObject(result));
}
}
/*
*當請求響應為String時
*/
public static String getString(String url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
String result = null;
try {
result = EntityUtils.toString(httpClient.execute(get).getEntity());
httpClient.close();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
return result;
}
}
}