在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订单定时导入---->请求失败");
}
大概就是这样,以后有别的东西再补充把