客戶端代碼 /** * 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報(bào)文結(jié)構(gòu) * @return */ String httpPost45(String url, String jsonParam) { //url?后面的即為post parmas 參數(shù),bodu 放在數(shù)據(jù)流中進(jìn)行傳輸 CloseableHttpClient httpclient = HttpClients.createDefault() // HttpGet httpGet = new HttpGet(url) HttpPost post=new HttpPost(url) //httpClient 4.5版本的超時(shí)參數(shù)配置 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(50000).setConnectionRequestTimeout(50000) .setSocketTimeout(50000).build() post.setConfig(requestConfig) //往BODY里填充數(shù)據(jù)主體 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("得到的結(jié)果:" + response.getStatusLine())//得到請(qǐng)求結(jié)果 String str = EntityUtils.toString(response.getEntity())//得到請(qǐng)求回來的數(shù)據(jù) return str }
POST_URL:請(qǐng)求url
urlParam:如上需要封裝進(jìn)url的參數(shù)
body:普通需要傳遞的參數(shù)
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(""); //將要拼接的參數(shù)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()); //請(qǐng)求參數(shù)設(shè)置 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?后面的參數(shù)集,同理,也可以將bodu放到參數(shù)集里,進(jìn)行傳輸 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數(shù)據(jù) ResponseMessage rsp = csmSvc.convertBodyAndSendErpRetu(apiInfo, customerInfo, body, "xml", params.id as Object, null) return rsp } 對(duì)于post參數(shù)流,服務(wù)端,可以這樣取值 String body = params.keySet()[0] + "=" + params[params.keySet()[0]].toString() params.keySet()[0]得到key params[params.keySet()[0]].toString()得到第一個(gè)key的value
OLY電子標(biāo)簽項(xiàng)目
import com.ittx.wms.api.service.ToolApiService import org.apache.http.Header import org.apache.http.HeaderElement import org.apache.http.HttpEntity import org.apache.http.ParseException import org.apache.http.client.entity.UrlEncodedFormEntity import org.apache.http.client.methods.CloseableHttpResponse import org.apache.http.client.methods.HttpPost import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClientBuilder import org.apache.http.message.BasicNameValuePair import org.apache.http.util.EntityUtils import org.springframework.beans.factory.annotation.Autowired import java.nio.charset.StandardCharsets /** * * Create by administrator 2021/4/12/0012 17:48 * **/ class Test { @Autowired ToolApiService taSvc public static void main(String[] args) { doPost("1", "1") } public static String doPost(String strUrl, String content) { CloseableHttpClient httpClient = HttpClientBuilder.create().build() HttpPost httpPost = new HttpPost("http://yun.zhuzhufanli.com/mini/select/"); CloseableHttpResponse response = null; try { httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded") List<BasicNameValuePair> param = new ArrayList<>() param.add(new BasicNameValuePair("appid", "160290")) param.add(new BasicNameValuePair("outerid", "7649974ED0D8499B")) param.add(new BasicNameValuePair("pageno", "1")) param.add(new BasicNameValuePair("taskname", "J210412_151338685")) UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(param, StandardCharsets.UTF_8) httpPost.setEntity(formEntity) response = httpClient.execute(httpPost) HttpEntity responseEntity = response.getEntity() println "HTTP響應(yīng)狀態(tài)為:" + response.getStatusLine() if (responseEntity != null) { println "HTTP響應(yīng)內(nèi)容長度為:" + responseEntity.getContentLength() String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) println "HTTP響應(yīng)內(nèi)容為:" + responseStr return responseStr } } catch (IOException e) { e.printStackTrace() } finally { try { if (httpClient != null) { httpClient.close() } if (response != null) { response.close() } } catch (IOException e) { e.printStackTrace() } } } }
聯(lián)系客服