1,post-Body流和post參數,以下客戶端代碼和服務端代碼可共用
客戶端代碼
/**
* post 方法
* 拋送給EDI
* @param url http://127.0.0.1:9003/api/edi/csm/csmReturnSubConBody?customerId=Fotile_CSM&api=csmreturnsub_confirm&id=6006
* @param jsonParam xml報文結構
* @return
*/
String httpPost45(String url, String jsonParam) {
//url?后面的即為post parmas 參數,bodu 放在數據流中進行傳輸
CloseableHttpClient httpclient = HttpClients.createDefault()
// HttpGet httpGet = new HttpGet(url)
HttpPost post=new HttpPost(url)
//httpClient 4.5版本的超時參數配置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(50000).setConnectionRequestTimeout(50000)
.setSocketTimeout(50000).build()
post.setConfig(requestConfig)
//往BODY里填充數據主體
StringEntity entitys=new StringEntity(jsonParam.toString(), "utf-8")
entitys.setContentEncoding("UTF-8")
entitys.setContentType("application/xml")
post.setEntity(entitys)
HttpResponse response = httpclient.execute(post)
// System.out.println("得到的結果:" + response.getStatusLine())//得到請求結果
String str = EntityUtils.toString(response.getEntity())//得到請求回來的數據
return str
}
客戶端代碼二=========================================

如果只是簡單拼接進url是行不通的,因為我們都知道URLEncoder,對url字符集編碼設置,所以需要對所有的值進行字符集編碼設置,最終我們封裝成了如下post方法支持url拼接入相應的請求參數:
POST_URL:請求url
urlParam:如上需要封裝進url的參數
body:普通需要傳遞的參數
public static String httpURLConnectionPOST (String POST_URL,Map<String, String> urlParam,String body) {
CloseableHttpResponse response = null;
try {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(6000)
.setConnectTimeout(6000)
.setConnectionRequestTimeout(6000)
.build();
//httpclient
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
// HttpPost httpPost = new HttpPost(POST_URL);
StringBuilder param=new StringBuilder("");
//將要拼接的參數urlencode
for (String key:urlParam.keySet()){
param.append(key + "=" + URLEncoder.encode(urlParam.get(key), "UTF-8") + "&");
}
//pingjie
HttpPost httpPost = new HttpPost(POST_URL+param.toString());
//請求參數設置
if(com.sf.ccsp.common.util.StringUtils.isNotEmpty(body)){
StringEntity entity=new StringEntity(body, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
}
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
} catch (ClientProtocolException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (Exception e){
System.out.println(e);
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return null;
}
服務端代碼
@ResponseBody
@RequestMapping(value = "csmReturnSubConBody", method = RequestMethod.POST, produces = "application/xml")
ResponseMessage csmReturnSubConBody(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map<String, String> params) {
//params為客戶端URL?后面的參數集,同理,也可以將bodu放到參數集里,進行傳輸
CustomerInfo customerInfo = erpSetting.getCustomerInfo(params.customerId as String)
if (!customerInfo) return
ApiInfo apiInfo = erpSetting.getApiInfo(customerInfo, params.api as String)
if (!apiInfo) return
String body = readBody(request)//這里去解析post流里的body數據
ResponseMessage rsp = csmSvc.convertBodyAndSendErpRetu(apiInfo, customerInfo, body, "xml", params.id as Object, null)
return rsp
}
對於post參數流,服務端,可以這樣取值
String body = params.keySet()[0] + "=" + params[params.keySet()[0]].toString()
params.keySet()[0]得到key
params[params.keySet()[0]].toString()得到第一個key的value
OLY電子標簽項目
