国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
HTTP
userphoto

2022.08.18 北京

關(guān)注

1,post-Body流和post參數(shù),以下客戶端代碼和服務(wù)端代碼可共用

  1. 客戶端代碼
  2. /**
  3. * post 方法
  4. * 拋送給EDI
  5. * @param url http://127.0.0.1:9003/api/edi/csm/csmReturnSubConBody?customerId=Fotile_CSM&api=csmreturnsub_confirm&id=6006
  6. * @param jsonParam xml報(bào)文結(jié)構(gòu)
  7. * @return
  8. */
  9. String httpPost45(String url, String jsonParam) {
  10. //url?后面的即為post parmas 參數(shù),bodu 放在數(shù)據(jù)流中進(jìn)行傳輸
  11. CloseableHttpClient httpclient = HttpClients.createDefault()
  12. // HttpGet httpGet = new HttpGet(url)
  13. HttpPost post=new HttpPost(url)
  14. //httpClient 4.5版本的超時(shí)參數(shù)配置
  15. RequestConfig requestConfig = RequestConfig.custom()
  16. .setConnectTimeout(50000).setConnectionRequestTimeout(50000)
  17. .setSocketTimeout(50000).build()
  18. post.setConfig(requestConfig)
  19. //往BODY里填充數(shù)據(jù)主體
  20. StringEntity entitys=new StringEntity(jsonParam.toString(), "utf-8")
  21. entitys.setContentEncoding("UTF-8")
  22. entitys.setContentType("application/xml")
  23. post.setEntity(entitys)
  24. HttpResponse response = httpclient.execute(post)
  25. // System.out.println("得到的結(jié)果:" + response.getStatusLine())//得到請(qǐng)求結(jié)果
  26. String str = EntityUtils.toString(response.getEntity())//得到請(qǐng)求回來的數(shù)據(jù)
  27. return str
  28. }

客戶端代碼二=========================================


如果只是簡(jiǎn)單拼接進(jìn)url是行不通的,因?yàn)槲覀兌贾繳RLEncoder,對(duì)url字符集編碼設(shè)置,所以需要對(duì)所有的值進(jìn)行字符集編碼設(shè)置,最終我們封裝成了如下post方法支持url拼接入相應(yīng)的請(qǐng)求參數(shù):

POST_URL:請(qǐng)求url
urlParam:如上需要封裝進(jìn)url的參數(shù)
body:普通需要傳遞的參數(shù)

  1. public static String httpURLConnectionPOST (String POST_URL,Map<String, String> urlParam,String body) {
  2. CloseableHttpResponse response = null;
  3. try {
  4. RequestConfig defaultRequestConfig = RequestConfig.custom()
  5. .setSocketTimeout(6000)
  6. .setConnectTimeout(6000)
  7. .setConnectionRequestTimeout(6000)
  8. .build();
  9.         //httpclient
  10. CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
  11. // HttpPost httpPost = new HttpPost(POST_URL);
  12. StringBuilder param=new StringBuilder("");
  13. //將要拼接的參數(shù)urlencode
  14. for (String key:urlParam.keySet()){
  15. param.append(key + "=" + URLEncoder.encode(urlParam.get(key), "UTF-8") + "&");
  16. }
  17. //pingjie
  18. HttpPost httpPost = new HttpPost(POST_URL+param.toString());
  19. //請(qǐng)求參數(shù)設(shè)置
  20. if(com.sf.ccsp.common.util.StringUtils.isNotEmpty(body)){
  21. StringEntity entity=new StringEntity(body, ContentType.APPLICATION_JSON);
  22. httpPost.setEntity(entity);
  23. }
  24. response = httpclient.execute(httpPost);
  25. HttpEntity entity = response.getEntity();
  26. return EntityUtils.toString(entity, "UTF-8");
  27. } catch (UnsupportedEncodingException e) {
  28. logger.error(e.getMessage(), e);
  29. } catch (ClientProtocolException e) {
  30. logger.error(e.getMessage(), e);
  31. } catch (IOException e) {
  32. logger.error(e.getMessage(), e);
  33. } catch (Exception e){
  34. System.out.println(e);
  35. }finally {
  36. if (response != null) {
  37. try {
  38. response.close();
  39. } catch (IOException e) {
  40. logger.error(e.getMessage(), e);
  41. }
  42. }
  43. }
  44. return null;
  45. }

服務(wù)端代碼

  1. @ResponseBody
  2. @RequestMapping(value = "csmReturnSubConBody", method = RequestMethod.POST, produces = "application/xml")
  3. ResponseMessage csmReturnSubConBody(HttpServletRequest request, HttpServletResponse response,
  4. @RequestParam Map<String, String> params) {
  5. //params為客戶端URL?后面的參數(shù)集,同理,也可以將bodu放到參數(shù)集里,進(jìn)行傳輸
  6. CustomerInfo customerInfo = erpSetting.getCustomerInfo(params.customerId as String)
  7. if (!customerInfo) return
  8. ApiInfo apiInfo = erpSetting.getApiInfo(customerInfo, params.api as String)
  9. if (!apiInfo) return
  10. String body = readBody(request)//這里去解析post流里的body數(shù)據(jù)
  11. ResponseMessage rsp = csmSvc.convertBodyAndSendErpRetu(apiInfo, customerInfo, body, "xml", params.id as Object, null)
  12. return rsp
  13. }
  14. 對(duì)于post參數(shù)流,服務(wù)端,可以這樣取值
  15. String body = params.keySet()[0] + "=" + params[params.keySet()[0]].toString()
  16. params.keySet()[0]得到key
  17. params[params.keySet()[0]].toString()得到第一個(gè)key的value

OLY電子標(biāo)簽項(xiàng)目

2 httpPost form 表單提交 application/x-www-form-urlencoded

  1. import com.ittx.wms.api.service.ToolApiService
  2. import org.apache.http.Header
  3. import org.apache.http.HeaderElement
  4. import org.apache.http.HttpEntity
  5. import org.apache.http.ParseException
  6. import org.apache.http.client.entity.UrlEncodedFormEntity
  7. import org.apache.http.client.methods.CloseableHttpResponse
  8. import org.apache.http.client.methods.HttpPost
  9. import org.apache.http.impl.client.CloseableHttpClient
  10. import org.apache.http.impl.client.HttpClientBuilder
  11. import org.apache.http.message.BasicNameValuePair
  12. import org.apache.http.util.EntityUtils
  13. import org.springframework.beans.factory.annotation.Autowired
  14. import java.nio.charset.StandardCharsets
  15. /**
  16. *
  17. * Create by administrator 2021/4/12/0012 17:48
  18. *
  19. **/
  20. class Test {
  21. @Autowired
  22. ToolApiService taSvc
  23. public static void main(String[] args) {
  24. doPost("1", "1")
  25. }
  26. public static String doPost(String strUrl, String content) {
  27. CloseableHttpClient httpClient = HttpClientBuilder.create().build()
  28. HttpPost httpPost = new HttpPost("http://yun.zhuzhufanli.com/mini/select/");
  29. CloseableHttpResponse response = null;
  30. try {
  31. httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded")
  32. List<BasicNameValuePair> param = new ArrayList<>()
  33. param.add(new BasicNameValuePair("appid", "160290"))
  34. param.add(new BasicNameValuePair("outerid", "7649974ED0D8499B"))
  35. param.add(new BasicNameValuePair("pageno", "1"))
  36. param.add(new BasicNameValuePair("taskname", "J210412_151338685"))
  37. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(param, StandardCharsets.UTF_8)
  38. httpPost.setEntity(formEntity)
  39. response = httpClient.execute(httpPost)
  40. HttpEntity responseEntity = response.getEntity()
  41. println "HTTP響應(yīng)狀態(tài)為:" + response.getStatusLine()
  42. if (responseEntity != null) {
  43. println "HTTP響應(yīng)內(nèi)容長度為:" + responseEntity.getContentLength()
  44. String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8)
  45. println "HTTP響應(yīng)內(nèi)容為:" + responseStr
  46. return responseStr
  47. }
  48. } catch (IOException e) {
  49. e.printStackTrace()
  50. } finally {
  51. try {
  52. if (httpClient != null) {
  53. httpClient.close()
  54. }
  55. if (response != null) {
  56. response.close()
  57. }
  58. } catch (IOException e) {
  59. e.printStackTrace()
  60. }
  61. }
  62. }
  63. }
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
spring集成httpclient連接池配置
HttpClient-aaaaa
springboot 整合httpclient【面試+工作】
HttpClient工具類
關(guān)于調(diào)用接口 Connection reset 問題(使用代理調(diào)接口)
JAVA利用HttpClient進(jìn)行POST請(qǐng)求(HTTPS)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服