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的更多相关文章
随机推荐
- latex编译过程-关于嵌入所有字体
我们的初始目的是想在编译的过程中嵌入所有字体 参考 我们进行了设置,但是不起作用,后发现使用pdflatex编译时是不会调用 ps2pdf的 然后,我们就需要了解编译过程 1. 通常,我们使用texs ...
- Linux下安装二进制版mysql-8.0.15
1.添加用户## 添加用户组groupadd mysql## 添加用户,指定用户home目录useradd -g mysql mysql -d /data/mysql## 解压下载的mysql二进制包 ...
- 关于在linux python源文件头部添加 “#!/usr/bin/env python” 不能直接运行的问题
如果环境变量设置正确 如果文件是从windows拷贝到linux中的 可能是换行符造成的.试试dos2unix命令,或相似的命令,把dos格式的换行符转为unix格式.
- 基于OpenCV的面部交换
需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...
- 【Lintcode】070.Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Vue cli项目开启Gzip
目录 安装 compression-webpack-plugin 更改配置文件 服务器开启gzip功能 安装 compression-webpack-plugin 建议安装v1.1.11版本,最新版本 ...
- C# 架构模式
单例模式 (Singleton) 单例讲的是当一个类被初次调用时,会产生一个类的实例, 而这个类的实例会贯穿程序的整个生命周期.单例提供了一个全局.唯一的实例. 步骤:1.让类自己创建一个实例:2.提 ...
- DataGrid 显示选中的item
Datagrid或者listview 中想要把相应的项 滚动到当前可见的位置, 必须满足2个条件: 1) 必须去掉虚拟化 VirtualizingStackPanel.IsVirtualiz ...
- MMU的理解
MMU内存管理单元相关知识点总结 1.MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚 ...
- Spring 3.1新特性之三:Spring支持Servlet 3.0(待补充)
高效并发是JDK 1.6的一个重要主题,HotSpot虚拟机开发团队在这个版本上花费了大量的精力去实现各种锁优化技术,如适应性自旋(Adaptive Spinning).锁削除(Lock Elimin ...