MyIfmHttpClient
生活随笔
收集整理的這篇文章主要介紹了
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";@Overridepublic 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());}}}
?
轉載于:https://www.cnblogs.com/tonggc1668/p/7525304.html
總結
以上是生活随笔為你收集整理的MyIfmHttpClient的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ 3697: 采药人的路径 [点
- 下一篇: 算法入门经典-第七章 例题7-2最大乘积