下面第一个 是json类型的
public static String send(String url,String data) throws DJException{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = null;
CloseableHttpResponse response=null;
try {
post = new HttpPost(url); // 请求方法Post
StringEntity entity = new StringEntity(data,"utf-8");
entity.setContentEncoding("UTF-8"); // 字符编码
entity.setContentType("application/json"); // json类型
post.setEntity(entity);
response =httpClient.execute(post);
int statusCode= response.getStatusLine().getStatusCode(); // 返回的状态码
if(statusCode == 200) {
HttpEntity he = response.getEntity();
return EntityUtils.toString(he, "UTF-8");
}else{
throw new DJException("服务器连接异常statusCode:"+statusCode);
}
} catch (IOException e) {
e.printStackTrace();
throw new DJException("请求失败:"+e.getMessage());
}finally {
try {
response.close();
httpClient.close();
} catch (IOException e) {
}
}
}
第二个是传入的参数是表单类型的 两种方式,区别不大
类型为from-data数据格式的请求方式
/**
* 类型为from-data数据格式的请求方式
* @param url
* @return
* @throws Exception
*/
public static String send(String url,Map<String,String> map) throws Exception{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = null;
CloseableHttpResponse response=null;
try {
post = new HttpPost(url);
// params添加表单数据 作为参数
List<NameValuePair> params = new ArrayList();
for (String parameter: map.keySet()) {
params.add(new BasicNameValuePair(parameter, map.get(parameter)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
post.setEntity(entity);
response =httpClient.execute(post);
int statusCode= response.getStatusLine().getStatusCode();
if(statusCode == 200) {
HttpEntity he = response.getEntity();
return EntityUtils.toString(he, "UTF-8");
}else{
throw new DJException("服务器连接异常statusCode:"+statusCode);
}
} catch (IOException e) {
e.printStackTrace();
throw new DJException("请求失败:"+e.getMessage());
}finally {
try {
response.close();
httpClient.close();
} catch (IOException e) {
}
}
}
/*
* **HttpClient Post 以表单提交方式请求 带参数**
*/
public static String send2(String url,Map<String,Object> map) throws DJException, IOException{
//1、创建HttpClient
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
//2、创建get或post请求方法
PostMethod method = new PostMethod(url);
//3、设置编码
httpClient.getParams().setContentCharset("UTF-8");
//4、设置请求消息头,为表单方式提交
method.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
//5、设置参数
//method.setRequestHeader(key,value);
method.setParameter(entry.getKey(), entry.getValue().toString());
}
// 6、执行提交
httpClient.executeMethod(method);
int statusCode= method.getStatusLine().getStatusCode();
if(statusCode == 200) {
String responseBodyAsString = method.getResponseBodyAsString();
return responseBodyAsString;
}else{
throw new DJException("服务器连接异常statusCode:"+statusCode);
}
}