JavaHttp接口交互的两种方式
JavaHttp接口开发针对表单提交跟Json提交的两种方式
表单提交(post、get)
get表单参数提交
/**
* @param type
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Get有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String HttpSendGet(String url, Map map)
throws ClientProtocolException, IOException{
String resultStr = null;
if(map==null){
return resultStr;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
List formparams = new ArrayList();
for (Map.Entry entry : map.entrySet()) {
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(formparams, Consts.UTF_8));
HttpGet httpget = new HttpGet(url+"?"+paramStr);
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr = EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
post表单提交
/**
* @param url
* @param map
* @return
* GY
* 2018年1月2日
* url:请求地址
* map:请求参数
* Post有参请求
* @throws IOException
* @throws ClientProtocolException
*/
public static String HttpSendPost(String url, Map map)
throws ClientProtocolException, IOException{
if(map==null){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
List formparams = new ArrayList();
for (Map.Entry entry : map.entrySet()) {
//给参数赋值
//gson.toJson(entry.getValue())
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
//System.out.println(EntityUtils.toString(entity));
//2.生成一个post请求
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httppost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr = EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}
get无参提交
/**
* 发送HttpGet请求
* @param url
* @return
* @throws IOException
* @throws ClientProtocolException
* GY
* 2018年1月2日
* Get无参请求
*/
public static String sendGet(String url) throws ClientProtocolException, IOException {
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//2.生成一个get请求
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
//3.执行get请求并返回结果
response = httpclient.execute(httpget);
String result = null;
//4.处理结果,这里将结果返回为字符串
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return result;
}
Json数据类型提交
/**
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
* GY
* 2018年1月4日
* 发送json 数据 http client
*/
public static String HttpSendJsonPost(String url, String json)
throws ClientProtocolException, IOException{
if(StringUtils.isEmpty(json)){
return null;
}
String resultStr = null;
//1.获得一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
StringEntity postingString = new StringEntity(json, Consts.UTF_8);// json传递
postingString.setContentEncoding("UTF-8");
postingString.setContentType("application/json");
System.out.println(EntityUtils.toString(postingString));
//2.生成一个post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(postingString);
//3.执行post请求并返回结果
CloseableHttpResponse response = httpclient.execute(httpPost);
//4.处理结果,这里将结果返回为字符串
HttpEntity responseEntity = response.getEntity();
if(responseEntity != null){
resultStr = EntityUtils.toString(responseEntity);
}
if(httpclient!=null){
httpclient.close();
}
if(response!=null){
response.close();
}
return resultStr;
}