NameValuePair方式傳參數


今天工作中聯調外部的一個接口用post方式傳輸,我按照文檔封裝參數成Jason字符串傳入,但是對方一直接受參數為空,折騰了半天也沒找到問題。很苦惱,檢查代碼都沒有錯誤,可是為什么對方接受參數為空呢?然后找對方的技術人員聯調,看看是怎么回事,也折騰了半天最后發現對方是用NameValuePair方式傳參數。雖然這個方式已經過時了,但是在這里記錄下,以備以后出現類似的方式傳參數。

 1 package com.souche.lease.finance.product;
 2 
 3 import com.alibaba.fastjson.JSON;
 4 import com.alibaba.fastjson.JSONObject;
 5 import org.apache.http.HttpStatus;
 6 import org.apache.http.NameValuePair;
 7 import org.apache.http.client.entity.UrlEncodedFormEntity;
 8 import org.apache.http.client.methods.CloseableHttpResponse;
 9 import org.apache.http.client.methods.HttpPost;
10 import org.apache.http.impl.client.CloseableHttpClient;
11 import org.apache.http.impl.client.HttpClients;
12 import org.apache.http.message.BasicNameValuePair;
13 import org.apache.http.protocol.HTTP;
14 import org.apache.http.util.EntityUtils;
15 
16 import java.io.IOException;
17 import java.io.UnsupportedEncodingException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 
23 /**
24  * Created by hunt on 2017/6/26.
25  * 使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
26  * 1. 創建HttpClient對象。
27  * 2. 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
28  * 3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HttpParams params)方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。
29  * 4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
30  * 5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
31  * 6. 釋放連接。無論執行方法是否成功,都必須釋放連接
32  */
33 public class SDTestDemo {
34     public static void main(String[] args) {
35         String licenseNo = "浙A588AX";
36         String token = "7cc2bd72eb1e4522804dca3b88e8644d";
37         String city = "330100";
38         String timestamp = Long.toString(System.currentTimeMillis());
39         String sign;
40         Map<String, Object> mapParam = new HashMap<>();
41         mapParam.put("licenseNo", licenseNo);
42         mapParam.put("token", token);
43         mapParam.put("city", city);
44         mapParam.put("timestamp", timestamp);
45         SDTestUtil testUtil = new SDTestUtil();
46         sign = testUtil.MD5(testUtil.sort(mapParam));
47         mapParam.put("sign", sign);
48         /**
49          * 定義了一個list,該list的數據類型是NameValuePair(簡單名稱值對節點類型),
50          * 這個代碼用於Java像url發送Post請求。在發送post請求時用該list來存放參數。
51          */
52         List<NameValuePair> urlParameters = new ArrayList<>();
53         urlParameters.add(new BasicNameValuePair("licenseNo", licenseNo));
54         urlParameters.add(new BasicNameValuePair("token", token));
55         urlParameters.add(new BasicNameValuePair("city", city));
56         urlParameters.add(new BasicNameValuePair("timestamp", timestamp));
57         urlParameters.add(new BasicNameValuePair("sign", sign));
58         CloseableHttpClient httpclient = HttpClients.createDefault();
59         CloseableHttpResponse response = null;
60         HttpPost post = new HttpPost("http://101.231.154.154:8047/v4.0/renewal");
61         try {
62             post.setEntity(new UrlEncodedFormEntity(urlParameters, HTTP.UTF_8));
63             try {
64                 response = httpclient.execute(post);
65                 // 判斷網絡連接狀態碼是否正常(0--200都數正常)
66                 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
67                     String content = EntityUtils.toString(response.getEntity(), "UTF-8");
68                     JSONObject jsonObject = JSON.parseObject(content);
69                     System.out.println("報價返回內容是:" + jsonObject.toString());
70                     if ("SUCCESS".equals(jsonObject.getString("code"))) {
71                         System.out.println("成功,系統處理正常");
72                     }
73                 }
74                 EntityUtils.consume(response.getEntity());//完全消耗
75             } catch (IOException e) {
76                 e.printStackTrace();
77             } finally {
78                 try {
79                     if (null != response) response.close();
80                 } catch (IOException e) {
81                     e.printStackTrace();
82                 }
83             }
84         } catch (UnsupportedEncodingException e) {
85             e.printStackTrace();
86         } finally {
87             //釋放鏈接
88             try {
89                 httpclient.close();
90             } catch (IOException e) {
91                 e.printStackTrace();
92             }
93         }
94     }
95 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM