Java HttpClient在外围接口调用实例

来源:互联网 发布:手机淘宝怎么清除缓存 编辑:程序博客网 时间:2024/05/16 11:13

封装一个HttpClient类:

package com.cyberwise.crmss.util;


import java.io.IOException;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.util.EntityUtils;
/**
 * 外围接口调用
 * @author dell
 *
 */
public class HttpClient {
private String name;//调用接口中文名字
private String url = "http://111.8.10.103:8082/channelTest?isconvert=true&action=";

public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getUrl() {
return url;
}


public void setUrl(String url) {
this.url = url;
}


public String post(Object name,String url, String body){
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
   try {
    httpClient = HttpClients.createDefault();
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); 
    httpPost = new HttpPost(url);
    httpPost.setConfig(requestConfig);
    httpPost.setEntity(new StringEntity(body));
    CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity,"utf-8");
System.out.println(result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(httpPost!=null){
httpPost.releaseConnection();
}
if(httpClient!=null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
   return result;
}
}


在在Controller里建立一个,接口类:

/**
* 调用亚信公告查询接口
*/
public CyberResponseDataElement qryCentralNoticeList(ServletContext context,
CyberRequestDataElement request) {
CyberResponseDataElement ret = new CyberResponseDataElement(request);
Object name = request.getElement("NAME");//接口中文名字
Object interface_name = request.getElement("INTERFACE_NAME");//接口
Object LOGIN_NAME = request.getElement("LOGIN_NAME");//登录工号
Object START = request.getElement("START");//开始行
Object END = request.getElement("END");//结束行

if (name == null || "".equals(name.toString())) {
ret.setState(-1);
ret.setStateInfo("请输入参数[NAME]");
return ret;
}

if (interface_name == null || "".equals(interface_name.toString())) {
ret.setState(-1);
ret.setStateInfo("请输入参数[INTERFACE_NAME]");
return ret;
}

if (LOGIN_NAME == null || "".equals(LOGIN_NAME.toString())) {
ret.setState(-1);
ret.setStateInfo("请输入参数[LOGIN_NAME]");
return ret;
}

if (START == null || "".equals(START.toString())) {
ret.setState(-1);
ret.setStateInfo("请输入参数[START]");
return ret;
}

if (END == null || "".equals(END.toString())) {
ret.setState(-1);
ret.setStateInfo("请输入参数[END]");
return ret;
}
HttpClient httpclient = new HttpClient();
String url = httpclient.getUrl()+interface_name;

String json = "{\"loginName\":\""+LOGIN_NAME+"\",\"start\":\""+START+"\",\"end\":\""+END+"\",\"bean\":{\"contentType\":\"1\"}}";

String data = httpclient.post(name,url,json);
Map maps = (Map)JSON.parse(data);
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
result = (List<Map<String, Object>>) maps.get("beans");
if(result!=null){
ret.setState(0);
ret.setElement("NOTICE_LIST", result.toArray());
return ret;
}else{
ret.setState(-1);
ret.setStateInfo("调用接口失败!");
return ret;
}
}


再在客户端,调用接口qryCentralNoticeList得到数据:

注意:本案例针对不同的项目,得稍作改动,稍微变通一下就可以了。

原创粉丝点击