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 }

 

posted on 2017-06-23 17:37  默默红尘  阅读(35197)  评论(1编辑  收藏  举报

导航