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得到数据:
注意:本案例针对不同的项目,得稍作改动,稍微变通一下就可以了。
- Java HttpClient在外围接口调用实例
- java后台HttpClient调用http接口实例
- java使用httpClient调用接口
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- 使用httpclient实现http接口调用实例
- HttpClient方式调用接口的实例
- java HttpClient get post请求 调用接口
- Apache HttpClient 实现 Java 调用 Http 接口
- JAVA利用HttpClient进行HTTPS接口调用
- JAVA利用HttpClient进行HTTPS接口调用
- JAVA利用HttpClient进行HTTPS接口调用
- 《转》使用httpclient实现http接口调用实例
- HttpClient调用接口
- httpclient接口调用
- httpclient http接口调用
- httpClient 接口调用
- MAVEN 把 src.main 包下的 properties 打包到 jar里
- java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
- eclipse从数据库逆向生成Hibernate实体类
- Websocket使用之原理
- JeeSite项目部署遇到的问题(NoClassDefFoundError;jstl/core、tags/form、sitemesh/decorator cannot be resolved)
- Java HttpClient在外围接口调用实例
- 全国各省市最低工资sql
- CentOS 7 安装FFTW3
- Java代理(三) JDK动态代理
- 创建型模式—原型模式
- IO之File
- Typical Exploration Strategies in Model-free Policy Search
- 主机字节序
- 网站防止注入入侵的一些有效代码和方法