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的更多相关文章
随机推荐
- ubuntn下 apt的用法和yum的比较(转)
centos有yum安装软件,Ubuntu有apt工具. apt简单的来说,就是给Ubuntu安装软件的一种命令方式. 一.apt的相关文件 /etc/apt/sources.list 设置软件包的获 ...
- unable to create new native thread 问题
ulimit -a ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling pr ...
- listen 66
Frog Species Found in Big Apple Scientists discover new species all the time—on the order of 15,000 ...
- 剑指offer12 打印从1到N位的所有数字,处理大整数情况
/** * */ package jianzhioffer; /** * @Description 输入n位数,输出0-N的所有数 * @author liutao * @data 2016年4月22 ...
- C#工程引用dll如何配置
C#工程引用需要注意的事项: <ItemGroup Condition="'$(Configuration)|$(Platform)' == &a ...
- python mysqldb 教程
MySQL Python 教程 (1)下面是在Python中使用MySql数据库的教程,涵盖了Python对MySql的基础操作,主要采用了MySQLdb模块,下面的代码都是基于Ubuntu Linu ...
- 一步步实现 Prism + MEF(二)--- 绑定命令
Prism程序集为我们提供了DelegateCommand命令,使用该命令可实现窗口直接绑定.第一步:在ViewModel中定义一个DelegateCommand属性. public Delegate ...
- (二)搭建SSH环境
一.struts 1.添加jar包: commons-fileupload-1.3.1.jar,[文件上传相关包] commons-io-2.2.jar, commons-lang-2.4.jar , ...
- USB相关资料
http://www.usb.org/developers/defined_class/#BaseClass00h http://blog.csdn.net/lizzywu/article/detai ...
- 使用BIND安装智能DNS服务器(一)---基本的主从DNS服务器搭建
参考网址:http://www.unixmen.com/dns-server-installation-step-by-step-using-centos-6-3/ DNS(Domain Name S ...