1,post-Body流和post参数,以下客户端代码和服务端代码可共用
* @param url http://127.0.0.1:9003/api/edi/csm/csmReturnSubConBody?customerId=Fotile_CSM&api=csmreturnsub_confirm&id=6006 * @param jsonParam xml报文结构 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) StringEntity entitys=new StringEntity(jsonParam.toString(), "utf-8") entitys.setContentEncoding("UTF-8") entitys.setContentType("application/xml") HttpResponse response = httpclient.execute(post) // System.out.println("得到的结果:" + response.getStatusLine())//得到请求结果 String str = EntityUtils.toString(response.getEntity())//得到请求回来的数据
客户端代码二=========================================
如果只是简单拼接进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; RequestConfig defaultRequestConfig = RequestConfig.custom() .setConnectionRequestTimeout(6000) CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); // HttpPost httpPost = new HttpPost(POST_URL); StringBuilder param=new StringBuilder(""); for (String key:urlParam.keySet()){ param.append(key + "=" + URLEncoder.encode(urlParam.get(key), "UTF-8") + "&"); 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 (IOException e) { logger.error(e.getMessage(), e);
服务端代码
@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) String body = readBody(request)//这里去解析post流里的body数据 ResponseMessage rsp = csmSvc.convertBodyAndSendErpRetu(apiInfo, customerInfo, body, "xml", params.id as Object, null) String body = params.keySet()[0] + "=" + params[params.keySet()[0]].toString() params[params.keySet()[0]].toString()得到第一个key的value
OLY电子标签项目
2 httpPost form 表单提交 application/x-www-form-urlencoded
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 public static void main(String[] args) { public static String doPost(String strUrl, String content) { CloseableHttpClient httpClient = HttpClientBuilder.create().build() HttpPost httpPost = new HttpPost("http://yun./mini/select/"); CloseableHttpResponse response = null; 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响应状态为:" + response.getStatusLine() if (responseEntity != null) { println "HTTP响应内容长度为:" + responseEntity.getContentLength() String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8) println "HTTP响应内容为:" + responseStr } catch (IOException e) { if (httpClient != null) { } catch (IOException e) {
|