JAVA HttpClient post请求 get请求 通用方法
我用的是HttpClient
没有那么多废话 , 小二上代码
先来post请求带参数 ,带请求头
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Set;
public static String httpPost2(String url, String content, Map<String,Object> httpHeader) {
// 创建HttpPost
String result = null;
HttpClient httpClient;
// 创建连接
httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON + "");
if(httpHeader.size()>0 && httpHeader != null){//这里为请求头
Set<Map.Entry<String, Object>> entry = httpHeader.entrySet();
for(Map.Entry<String, Object> en:entry){
httpPost.setHeader(en.getKey(), String.valueOf(en.getValue()));
}
}
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
httpPost.setConfig(requestConfig);
BasicHttpEntity requestBody = new BasicHttpEntity();
requestBody.setContent(new ByteArrayInputStream(content.getBytes("utf-8")));
requestBody.setContentLength(content.getBytes("utf-8").length);
httpPost.setEntity(requestBody);
// 执行客户端请求
HttpEntity entity = httpClient.execute(httpPost).getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
}
} catch (Throwable e) {
e.printStackTrace();
logger.error("【HTTP请求失败】: url={}, content={}", url, content);
}
return result;
}
上面为方法,下面是用法
Map<String,Object> httpmap = new HashMap<String, Object>();
httpmap.put("HDC-Service","2");
httpmap.put("HDC-APPID",appid);
httpmap.put("HDC-Token",token);
LOGGER.info("请求头"+httpmap);
//url:请求路径 paramString:内容
// httpmap:请求头 如果不要加请求头 也要创建一个map传过去, 不然会报空异常
resultJson = HttpUtil.httpPost(url, paramString,httpmap);
接下来get请求
public static String httpGet(String url, Map<String,Object> httpHeader) {
// 创建Http
String result = null;
HttpClient httpClient ;
// 创建连接
httpClient = HttpClients.createDefault();
try {
HttpGet httpGet = new HttpGet(url);
if(httpHeader.size()>0){
Set<Map.Entry<String, Object>> entry = httpHeader.entrySet();
for(Map.Entry<String, Object> en:entry){
httpGet.setHeader(en.getKey(), String.valueOf(en.getValue()));
}
}
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpGet.setConfig(requestConfig);
// 执行客户端请求
HttpEntity entity = httpClient.execute(httpGet).getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
}
} catch (Throwable e) {
logger.error("【HTTP请求失败】: url={}", url);
}
return result;
}
用法都一样
注意:如果不要加请求头 也要创建一个map传过去, 不然会报空异常