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的更多相关文章
随机推荐
- linux 进程学习笔记-运行新进程
我们知道,当用fork启动一个新进程以后,新进程会复制父进程的大部份内存空间并接着运行父进程中的代码,如果我们使新进程不运行原父进程的代码,转而运行另外一个程序集中的代码,这就相当于启动了一个新程序. ...
- .DS_Store 文件是什么?Mac下面如何禁止.DS_Store生成
.DS_Store是Mac OS保存文件夹的自定义属性的隐藏文件,如文件的图标位置或背景色,相当于Windows的desktop.ini. 1,禁止.DS_store生成:打开 “终端” ,复制黏贴下 ...
- BZOJ_5338_ [TJOI2018]xor_可持久化trie
BZOJ_5338_ [TJOI2018]xor_可持久化trie Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...
- poj2777Count Color——线段树+状压
题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...
- cocos2dx unzip、createDir
转自:http://www.cnblogs.com/xioapingguo/p/4037323.html static unsigned long _maxUnzipBufSize = 0x50000 ...
- Java多线程运行机制的基本原理
Java多线程运行机制的基本原理 进程和线程的区别 进程 进程是一个程序执行的实例,比如说我们打开10个IE浏览器窗口,那么就有10个进程开启.一个进程可以同时被运行若干次,进程是CPU进行资源分配和 ...
- eclipse(java) 使用SQL …
java连接sqlserver2005数据库 首先得下载驱动程序到微软网站下载Microsoft JDBC Driver 4.0 for SQL Server 下载地址 :http://msdn. ...
- 来自word2013发布的测试文档
SWIG 是一个非常优秀的开源工具,支持您将 C/C++ 代码与任何主流脚本语言相集成. 此外,它向更广泛的受众公开了基本代码,改善了可测试性,让您的 Ruby 代码库某部分能快速写出高性能的 C/C ...
- 如何查看Mysql event事件是否启用
mysql> show variables like 'event_scheduler';+-----------------+-------+| Variable_name | Value ...
- backgroundWorker取消后,重新开始就报错:此 BackgroundWorker 当前正忙,无法同时运行多个任务。
使用BackgroundWorker控件,有2个按钮buttonBegin和buttonCancel.其他都正常,只是在用buttonBegin开始运行,然后点击buttonCancel取消后,到这里 ...