在Java中傳送http的post請求,設定請求引數等等
前幾天做了一個定時匯入資料的介面,需要傳送http請求,第一次做這種的需求,特地記一下子,
導包
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
1 CloseableHttpResponse response;// 響應類, 2 CloseableHttpClient httpClient = HttpClients.createDefault(); 3 HttpPost httpPost = new HttpPost(JTR_QUERY_ORDER); //請求url 4 5 // 轉json引數 6 String paramJson = JSONObject.fromObject(sortedMap).toString();7 StringEntity stringEntity = new StringEntity(paramJson,ContentType.create("text/json", "UTF-8")); 8 httpPost.setEntity(stringEntity); 9 10 response = httpClient.execute(httpPost); 11 12 //這種是傳送json請求引數的,傳送form形式引數的可以通過 13 14 SortedMap<String, String> sortedMap = packetRequestParameters(cash, orderNum, authCode, payType, mchId);15 16 List<NameValuePair> params = new ArrayList<NameValuePair>(sortedMap.size()); 17 if (!sortedMap.isEmpty()) { 18 for (Map.Entry<String, String> parameter : sortedMap.entrySet()) { 19 params.add(new BasicNameValuePair(parameter.getKey(), parameter 20 .getValue())); 21 } 22 } 23 httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); 24 25 //設定請求頭資訊可以通過 26 27 Map<String, String> headerMap = new HashMap<>(); 28 headerMap.put("X-QF-SIGN", sortedMap.get("sign")); 29 headerMap.put("X-QF-APPCODE", APP_CODE);//分配給開發者的app_code,開發者的唯一標示 30 31 if (!headerMap.isEmpty()) { 32 for (Map.Entry<String, String> vo : headerMap.entrySet()) { 33 httpPost.setHeader(vo.getKey(), vo.getValue()); 34 } 35 } 36 37 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 38 // 3 請求成功,處理請求結果 39 if (response != null && response.getEntity() != null) { 40 String string = EntityUtils.toString(response.getEntity(),"utf-8"); 41 // String unescapeJava = StringEscapeUtils.unescapeJava(string);//java反轉義,示例中不需要所以註釋了 42 JSONObject resultObject = JSONObject.fromObject(string);
// 如果是陣列型別的話使用 JSONArray resultObject = JSONArray.fromObject(fromBase64);
43 // log.info("定時匯入---->返回資料3: " + resultObject.toString()); 44 // 插入資料 45 flag = insertJtrOrder(resultObject); 46 }else{ 47 flag = false; 48 log.error("訂單定時匯入---->系統出錯"); 49 } 50 }else{ 51 flag = false; 52 log.error("訂單定時匯入---->請求失敗"); 53 }
CloseableHttpResponse response;// 響應類,
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(JTR_QUERY_ORDER); //請求url
// 轉json引數
String paramJson = JSONObject.fromObject(sortedMap).toString();
StringEntity stringEntity = new StringEntity(paramJson,ContentType.create("text/json", "UTF-8"));
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
這種是傳送json請求引數的,傳送form形式的可以通過
SortedMap<String, String> sortedMap = packetRequestParameters(cash, orderNum, authCode, payType, mchId);
List<NameValuePair> params = new ArrayList<NameValuePair>(sortedMap.size());
if (!sortedMap.isEmpty()) {
for (Map.Entry<String, String> parameter : sortedMap.entrySet()) {
params.add(new BasicNameValuePair(parameter.getKey(), parameter
.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
設定請求頭資訊可以通過
Map<String, String> headerMap = new HashMap<>();
headerMap.put("X-QF-SIGN", sortedMap.get("sign"));
headerMap.put("X-QF-APPCODE", APP_CODE);//分配給開發者的app_code,開發者的唯一標示
if (!headerMap.isEmpty()) {
for (Map.Entry<String, String> vo : headerMap.entrySet()) {
httpPost.setHeader(vo.getKey(), vo.getValue());
}
}
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 3 請求成功,處理請求結果
if (response != null && response.getEntity() != null) {
String string = EntityUtils.toString(response.getEntity(),"utf-8");
// String unescapeJava = StringEscapeUtils.unescapeJava(string);//java反轉義,示例中不需要所以註釋了
JSONObject resultObject = JSONObject.fromObject(string);
// log.info("JTR訂單定時匯入---->返回資料3: " + resultObject.toString());
// 插入資料
flag = insertJtrOrder(resultObject);
}else{
flag = false;
log.error("JTR訂單定時匯入---->系統出錯");
}
}else{
flag = false;
log.error("JTR訂單定時匯入---->請求失敗");
}
大概就是這樣,以後有別的東西再補充把