在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訂單定時導入---->請求失敗");
}

大概就是這樣,以后有別的東西再補充把


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM