public static JSONObject requestCdi(String url, Map params) {
log.info("url: {}", url);
log.debug("params: {}", params);
HttpRequest httpRequest = new HttpRequest(url);
httpRequest.setRequestCharset(Configure.get("cdiRequestUrl", "requestCharset"));
if (params != null && !params.isEmpty()) {
httpRequest.setParams(params);
}
String result = httpRequest.doPost();
log.debug("response result: {}", result);
JSONObject data = JSONObject.fromObject(result);
if (!data.getBoolean("success")) {
log.error("request cdi error, url: {}, result: {}", url, data);
}
return data;
}
public String doPost() {
String responseString = null;
CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(this.url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(95 * 1000)
.setConnectTimeout(100 * 1000).build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + this.requestCharset);
//httpPost.setHeader("Content-Type", "application/json; charset=" + this.requestCharset);
//httpPost.setHeader("Accept", "application/json");
if (this.params != null && !this.params.isEmpty()) {
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
Entry<String, String> entry = null;
for (Iterator<Entry<String, String>> iter = this.params.entrySet().iterator(); iter.hasNext();) {
entry = iter.next();
log.info("key is {} ",entry.getKey());
nameValuePairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
//entry.getValue()
}
log.info("nameValuePairList : {} ",nameValuePairList.toString());
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, this.requestCharset));
log.info("httpPost.getEntity() : {} ",httpPost.getEntity().toString());
}
HttpResponse httpResponse = httpclient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (this.responseCharset == null || "".equals(this.responseCharset)) {
this.responseCharset = getCharset(httpResponse);
}
if (isGzip(httpResponse)) {
responseString = EntityUtils.toString(new GzipDecompressingEntity(httpResponse.getEntity()),
this.responseCharset);
} else {
responseString = EntityUtils.toString(httpResponse.getEntity(), this.responseCharset);
}
}
} catch (Exception e) {
//log.error("Http post request error: {}", e.getMessage());
e.printStackTrace();
} finally {
try {
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
log.error(e.toString());
}
}
return responseString;
}
下面是spring mvc 接收 代码
@RequestMapping(value = { "/infoDayHits/{proSymbol:[A-Za-z]+}" }, method = {RequestMethod.GET,RequestMethod.POST})
@ResponseBody
public String addInfoDayHit(@PathVariable String proSymbol,String data) {
try {
log.info("add {} InfoDayHit start...", proSymbol);
log.info("data: {}", data);
//InfoDayHit infoDayHit = new InfoDayHit();
//ModelData modelData = JsonUtils.toObject(data, ModelData.class);
ModelData modelData = JsonUtils.toObject(data, ModelData.class);
// key: synchDate -- 2017-11-07 year:2017 mmdd: "1107"
// key: infoDayHitsData
// infoDayHitsList JsonUtils.toObject(infoDayHitsData, ModelData[].class)
// for each infoDayHitsList ModelData --key infoId; key dayHits
String year = modelData.get("synchDate").toString().substring(0,4);
String mmDD = modelData.get("synchDate").toString().substring(5).replace("-", "");
log.info("year: {}", year);
log.info("mmDD: {}", mmDD);
//modelData.get("infoDayHitsData")
@SuppressWarnings("rawtypes")
List hitData = (List) modelData.get("infoDayHitsData");
infoDayHitService.addInfoDayHit(hitData, proSymbol, year, mmDD);
log.info("add InfoDayHit end...");
//return R.success();
}catch (Exception e) {
log.error(e.toString());
}
return R.success();
}
接收到的 data 打印 为null,但是在别的服务器上 测试 是可以 接收到 参数的