MyIfmHttpClient
package com.yd.ifm.client.caller.util.http; import java.util.Map; import com.yd.ifm.client.caller.model.ResponseData;
import com.yd.ifm.client.caller.util.http.HttpEnum.ContentTypeEnum; public interface IfmHttpClient { /**
* 发送post数据
* 200为正常的业务数据,202为IfmClient的一些授权不通过或者异常信息
* headerMap 需要放在Http客户端的header中
* data 为body中的业务数据
* @param strUrlPath
* @param params
* @param encode
* @return
*/
ResponseData postData(String strUrlPath, Map<String, String> headerMap, String data, String encode, ContentTypeEnum contentType);
}
package com.yundaex.wms.config.clent; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map; import org.apache.log4j.Logger; import com.yd.ifm.client.caller.model.ResponseData;
import com.yd.ifm.client.caller.util.http.HttpEnum.ContentTypeEnum;
import com.yd.ifm.client.caller.util.http.HttpEnum.RequestMethodEnum;
import com.yd.ifm.client.caller.util.http.IfmHttpClient; /**
* <pre>
* Title: MyIfmHttpClient.java
* Description:
* Copyright: yundaex.com Copyright (c) 2017
* Company: 上海韵达货运有限公司
* </pre>
*
* @author tonglele
* @version 1.0
* @date 2017年9月15日
*/
public class MyIfmHttpClient implements IfmHttpClient {
private final static Logger log = Logger.getLogger(MyIfmHttpClient.class);
private final static String CONTENT_TYPE = "Content-Type";
private final static String CONTENT_LENGTH = "Content-Length";
private final static String ZERO = "0"; @Override
public ResponseData postData(String strUrlPath, Map<String, String> params, String data, String encode,
ContentTypeEnum contentType) {
byte[] bodybyte = getRequestData(data, encode);// 获得请求体
ResponseData responsedata = new ResponseData();
OutputStream outputStream = null;
InputStream inptStream = null;
try {
URL url = new URL(strUrlPath);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(20000); // 设置连接超时时间
httpURLConnection.setDoInput(true); // 打开输入流,以便从服务器获取数据
httpURLConnection.setDoOutput(true); // 打开输出流,以便向服务器提交数据
httpURLConnection.setRequestMethod(RequestMethodEnum.POST.getMethod()); // 设置以Post方式提交数据
httpURLConnection.setUseCaches(false); // 使用Post方式不能使用缓存
httpURLConnection.setReadTimeout(60000); // 设置读取数据的超时时间
// 添加控制权限的header
addHeader(params, httpURLConnection);
// 设置请求体的类型是文本类型
httpURLConnection.setRequestProperty(CONTENT_TYPE, contentType.getType());
// 设置请求体的长度
httpURLConnection.setRequestProperty(CONTENT_LENGTH,
bodybyte == null ? ZERO : String.valueOf(bodybyte.length));
// 获得输出流,向服务器写入数据
outputStream = httpURLConnection.getOutputStream();
if (bodybyte != null)
outputStream.write(bodybyte);
outputStream.flush(); int responsecode = httpURLConnection.getResponseCode(); // 获得服务器的响应码
responsedata.setError_code(responsecode);
// 200表示有正常的业务数据 202则表示有callee的异常
if (responsecode == HttpURLConnection.HTTP_OK || responsecode == 202) {
inptStream = httpURLConnection.getInputStream();
responsedata.setData(dealResponseResult(inptStream));
}
} catch (IOException e) {
log.error("error while using IfmHttpUtil" + e);
return responsedata;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error("error while using IfmHttpUtil" + e);
}
}
if (inptStream != null) {
try {
inptStream.close();
} catch (IOException e) {
log.error("error while using IfmHttpUtil" + e);
}
}
}
return responsedata;
} private byte[] getRequestData(String content, String encode) {
byte[] result = null;
try {
if (content != null)
result = content.getBytes(encode);
} catch (UnsupportedEncodingException e) {
log.error("error while using IfmHttpUtil" + e);
}
return result;
} /**
* 处理服务器返回结果
*
* @param inputStream
* 输入流
* @return 返回处理后的String 字符串
*/
private String dealResponseResult(InputStream inputStream) {
String resultData = null; // 存储处理结果
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
resultData = new String(byteArrayOutputStream.toByteArray(), "utf-8");
} catch (IOException e) {
log.error("error while using IfmHttpUtil" + e);
}
return resultData;
} /**
* 将权限信息放在header中
*
* @param headerMapper
* @param connection
*/
private void addHeader(Map<String, String> headerMapper, HttpURLConnection connection) {
for (Map.Entry<String, String> entry : headerMapper.entrySet()) {
connection.addRequestProperty(entry.getKey(), entry.getValue());
}
} }
MyIfmHttpClient的更多相关文章
随机推荐
- 最近火狐浏览器 总是“插件 adobe flash 已崩溃”
原因和解决方案:在地址栏中输入:about:addons>在如下地方发现firefox已经在警告该插件的安全性了>选择“总不激活”
- Jmeter-配置原件-HTTP Cookie管理器
线程组右键 -- 添加 -- 配置原件 -- HTTP Cookie管理器 如何定位到自己的cookie?以Google Chrome浏览器为例: 1.打开浏览器,打开开发者工具 2.登录站点 3 ...
- XML配置中 引入外部property文件
新建properties配置文件 内容如下: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\ ...
- kettle监控销售人员当月每天任务完成率_20161107周一
1.上面是目标表,其中激活客户数为当月每天之前30天未下单的客户 2.写SQL SELECT a.销售员,c.当月销售确认额,a.当月订单额,b.当月首单数,b.当月激活数, a1,b.b1,b.c1 ...
- BZOJ_2064_分裂_状压DP
BZOJ_2064_分裂_状压DP Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力. 同时经常搞OI的 ...
- 高级Java工程师必备 ----- 深入分析 Java IO (一)BIO
BIO编程 最原始BIO 网络编程的基本模型是C/S模型,即两个进程间的通信. 服务端提供IP和监听端口,客户端通过连接操作想服务端监听的地址发起连接请求,通过三次握手连接,如果连接成功建立,双方就可 ...
- 百度地图API的第一次接触——地图事件
0.初始化地图 var map = new BMap.Map("container"); var point = new BMap.Point(116.404, 39.915); ...
- JAVA操作cassandra数据库
如果是maven项目,在pom.xml里加入依赖.不是的话下载相应的jar包放到lib目录下.这里驱动包的版本要和你cassandra的大版本一致.我这里cassandra的版本是最新的3.9,驱动是 ...
- Linux 运维面试问题总结
Linux 运维面试问题总结 1.详细描述mysql主从复制的方式?(类似跟DRBD高可用的协议类型) 答: (1)同步复制(C协议:sync):只有在本地和远程磁盘都确定写入已完成时,主节点才会认为 ...
- JavaScript高级程序设计学习笔记第十四章--表单
1.在 HTML 中,表单是由<form>元素来表示的,而在 JavaScript 中,表单对应的则是 HTMLFormElement 类型. HTMLFormElement 继承了 HT ...