1. 程式人生 > >極光客戶互動雲java post請求

極光客戶互動雲java post請求

package com.common.utils;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; public class HttpRequestUtils { // private static Logger logger = LoggerFactory.getLogger(HttpRequestUtils.class);
//日誌記錄 /** * httpPost * @param url 路徑 * @param jsonParam 引數 * @return */ public static JSONObject httpPost(String url,JSONObject jsonParam){ return httpPost(url, jsonParam, false); } /** * httpPost * @param url 路徑 *
@param jsonParam 引數 * @return */ public static JSONObject httpPost(String url,JSONObject jsonParam, JSONObject header){ return httpPost(url, jsonParam, header, false); } /** * post請求 * @param url url地址 * @param jsonParam 引數 * @param noNeedResponse 不需要返回結果 * @return */ public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){ //post請求返回結果 HttpClient httpClient = HttpClientBuilder.create().build(); JSONObject jsonResult = null; HttpPost method = new HttpPost(url); try { if (null != jsonParam) { //解決中文亂碼問題 StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); method.setEntity(entity); } HttpResponse result = httpClient.execute(method); url = URLDecoder.decode(url, "UTF-8"); /**請求傳送成功,並得到響應**/ if (result.getStatusLine().getStatusCode() == 200) { String str = ""; try { /**讀取伺服器返回過來的json字串資料**/ str = EntityUtils.toString(result.getEntity()); if (noNeedResponse) { return null; } /**把json字串轉換成json物件**/ jsonResult = JSONObject.parseObject(str); } catch (Exception e) { // logger.error("post請求提交失敗:" + url, e); } } } catch (IOException e) { // logger.error("post請求提交失敗:" + url, e); } return jsonResult; } /** * post請求 * @param url url地址 * @param jsonParam 引數 * @param noNeedResponse 不需要返回結果 * @return */ public static JSONObject httpPost(String url,JSONObject jsonParam, JSONObject header, boolean noNeedResponse){ //post請求返回結果 HttpClient httpClient = HttpClientBuilder.create().build(); JSONObject jsonResult = null; HttpPost method = new HttpPost(url); for (Map.Entry<String, Object> entry : header.entrySet()) { method.setHeader(entry.getKey(), entry.getValue().toString()); } try { if (null != jsonParam) { //解決中文亂碼問題 StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); method.setEntity(entity); } HttpResponse result = httpClient.execute(method); url = URLDecoder.decode(url, "UTF-8"); /**請求傳送成功,並得到響應**/ if (result.getStatusLine().getStatusCode() == 200) { String str = ""; try { /**讀取伺服器返回過來的json字串資料**/ str = EntityUtils.toString(result.getEntity()); if (noNeedResponse) { return null; } /**把json字串轉換成json物件**/ jsonResult = JSONObject.parseObject(str); } catch (Exception e) { // logger.error("post請求提交失敗:" + url, e); } } } catch (IOException e) { // logger.error("post請求提交失敗:" + url, e); } return jsonResult; } /** * 傳送get請求 * @param url 路徑 * @return */ public static JSONObject httpGet(String url){ //get請求返回結果 JSONObject jsonResult = null; try { HttpClient client = HttpClientBuilder.create().build(); //傳送get請求 HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); /**請求傳送成功,並得到響應**/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /**讀取伺服器返回過來的json字串資料**/ String strResult = EntityUtils.toString(response.getEntity()); /**把json字串轉換成json物件**/ jsonResult = JSONObject.parseObject(strResult); url = URLDecoder.decode(url, "UTF-8"); } else { // logger.error("get請求提交失敗:" + url); } } catch (IOException e) { // logger.error("get請求提交失敗:" + url, e); } return jsonResult; } }