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的更多相关文章
随机推荐
- redis数据
毫无疑问,Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象 ...
- 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP
题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...
- ctypes模块与pywin32模块
ctypes模块: 主要用于调用c动态链接库. 1.聊聊Python ctypes模块 2.ctypes模块管理 相关网址: pywin32模块: 用于访问win32API函数(win32api模块) ...
- C语言中的文件操作
按照字符的方式读取文件 按照行的方式读取文件 按照数据块的方式读取文件 按照格式化的方式读取文件 文件分类 记录文件:具有一定的结构记录组成,分为定长和不定长两种方式 流式文件:按照一个字符一个字符( ...
- asteris录音设置
[macro-recording] exten =>s,1,Set(CALLFILENAME=${STRFTIME(${EPOCH},UTC-8,%Y-%m-%d-%H-%M-%S)}-${CA ...
- 华为USG6500系列
华为USG6500: ssh 登录配置 time-range 相关配置:<USG6000V1>system-view Enter system view, return user view ...
- python爬虫知识点总结(二)爬虫的基本原理
一.什么是爬虫? 答:请求网页并提取数据的自动化程序. 二.爬虫的基本流程 三.什么是Request和Response? 1.Request 2.Response 四.能抓取怎样的数据 五.解析方式 ...
- jQuery 验证 Validation
jQuery Validation 目录 简介: Form validation made easy. Validate a simple comment form with inline rules ...
- mina2中的线程池
一.Mina中的线程池模型 前面介绍了Mina总体的层次结构,那么在Mina里面是怎么使用Java NIO和进行线程调度的呢?这是提高IO处理性能的关键所在.Mina的线程调度原理主要如下图所示: A ...
- HDU 5547 Sudoku (暴力)
题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...