httpClient版本为4.5.3
建议通过HttpGet获取信息,HttpPost提交信息,而HttpGet获取信息时需要提交的参数一般会在url中体现,或者以? &传参,所以就没写HttpGet带参数的。
添加依赖
<!--http-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
代码实现
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
private static final CloseableHttpClient httpClient = HttpClients.createDefault();
private static final RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(2000)
.setSocketTimeout(10000).build();
/**
* GET请求 参数携带在url中
*
* @param url
* @return
*/
public static String executeGet(String url) {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
String result = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* POST请求 带参数
*
* @param url 请求地址
* @param map 参数
* @return
*/
public static String executePost(String url, Map<String, String> map) {
List<NameValuePair> params = new ArrayList<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = null;
String result = null;
try {
entity = new UrlEncodedFormEntity(params, "UTF-8");
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* POST请求 不带参数
*
* @param url
* @return
*/
public static String executePost(String url) {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
CloseableHttpResponse response = null;
String result = null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* POST请求 携带Json格式的参数
*
* @param url
* @param param
* @return
* @throws IOException
*/
public static String postJson(String url, Object param) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setConfig(requestConfig);
String parameter = JSON.toJSONString(param);
StringEntity se = null;
try {
System.out.println(parameter);
se = new StringEntity(parameter);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httpPost.setEntity(se);
CloseableHttpResponse response = null;
String result = null;
try {
response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}