HTTP简单介绍:
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
import org.apache.http.NameValuePair;
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.client.utils.URIBuilder;
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 org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author :feiyang
* @Date :Created in 2019/5/15
*/
public class HttpClientUtil {
private static CloseableHttpClient httpClient = HttpClients.createDefault();
/**
* POST 有参
* @param host
* @param port
* @param uri
* @param paramsMap
* @return
*/
public static String sendPost(String host, String port, String uri, Map<String, Object> paramsMap){
try {
String url = "http://"+host+":"+port+"/"+uri;
HttpPost post = new HttpPost(url);
if (!StringUtils.isEmpty(paramsMap)){
List<NameValuePair> params = new ArrayList<>();
for (Map.Entry<String, Object> entry : paramsMap.entrySet()
) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(formEntity);
}
CloseableHttpResponse response = httpClient.execute(post);
if (!StringUtils.isEmpty(response.getEntity())){
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* POST 无参
* @param host
* @param port
* @param uri
* @return
*/
public static String sendPost(String host, String port, String uri){
try {
String url = "http://"+host+":"+port+"/"+uri;
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = httpClient.execute(post);
if (!StringUtils.isEmpty(response.getEntity())){
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* GET 有参
* @param host
* @param port
* @param uri
* @param paramsMap
* @return
*/
public static String sendGet(String host, String port, String uri, Map<String, Object> paramsMap){
try {
String url = "http://"+host+":"+port+"/"+uri;
URIBuilder uriBuilder = new URIBuilder(url);
if (paramsMap != null) {
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getEntity() != null) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* GET 无参
* @param host
* @param port
* @param uri
* @return
*/
public static String sendGet(String host, String port, String uri){
try {
String url = "http://"+host+":"+port+"/"+uri;
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
if (response.getEntity() != null) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}