MyIfmHttpClient
生活随笔
收集整理的這篇文章主要介紹了
MyIfmHttpClient
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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 {/*** 發(fā)送post數(shù)據(jù)* 200為正常的業(yè)務(wù)數(shù)據(jù),202為IfmClient的一些授權(quán)不通過(guò)或者異常信息* headerMap 需要放在Http客戶端的header中* data 為body中的業(yè)務(wù)數(shù)據(jù)* @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: 上海韻達(dá)貨運(yùn)有限公司* </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";@Overridepublic ResponseData postData(String strUrlPath, Map<String, String> params, String data, String encode,ContentTypeEnum contentType) {byte[] bodybyte = getRequestData(data, encode);// 獲得請(qǐng)求體ResponseData responsedata = new ResponseData();OutputStream outputStream = null;InputStream inptStream = null;try {URL url = new URL(strUrlPath);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(20000); // 設(shè)置連接超時(shí)時(shí)間httpURLConnection.setDoInput(true); // 打開(kāi)輸入流,以便從服務(wù)器獲取數(shù)據(jù)httpURLConnection.setDoOutput(true); // 打開(kāi)輸出流,以便向服務(wù)器提交數(shù)據(jù)httpURLConnection.setRequestMethod(RequestMethodEnum.POST.getMethod()); // 設(shè)置以Post方式提交數(shù)據(jù)httpURLConnection.setUseCaches(false); // 使用Post方式不能使用緩存httpURLConnection.setReadTimeout(60000); // 設(shè)置讀取數(shù)據(jù)的超時(shí)時(shí)間// 添加控制權(quán)限的header
addHeader(params, httpURLConnection);// 設(shè)置請(qǐng)求體的類型是文本類型
httpURLConnection.setRequestProperty(CONTENT_TYPE, contentType.getType());// 設(shè)置請(qǐng)求體的長(zhǎng)度
httpURLConnection.setRequestProperty(CONTENT_LENGTH,bodybyte == null ? ZERO : String.valueOf(bodybyte.length));// 獲得輸出流,向服務(wù)器寫入數(shù)據(jù)outputStream = httpURLConnection.getOutputStream();if (bodybyte != null)outputStream.write(bodybyte);outputStream.flush();int responsecode = httpURLConnection.getResponseCode(); // 獲得服務(wù)器的響應(yīng)碼
responsedata.setError_code(responsecode);// 200表示有正常的業(yè)務(wù)數(shù)據(jù) 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;}/*** 處理服務(wù)器返回結(jié)果* * @param inputStream* 輸入流* @return 返回處理后的String 字符串*/private String dealResponseResult(InputStream inputStream) {String resultData = null; // 存儲(chǔ)處理結(jié)果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;}/*** 將權(quán)限信息放在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());}}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/tonggc1668/p/7525304.html
總結(jié)
以上是生活随笔為你收集整理的MyIfmHttpClient的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: BZOJ 3697: 采药人的路径 [点
- 下一篇: 算法入门经典-第七章 例题7-2最大乘积