java namevaluepair_NameValuePair方式传参数

1 packagecom.souche.lease.finance.product;2

3 importcom.alibaba.fastjson.JSON;4 importcom.alibaba.fastjson.JSONObject;5 importorg.apache.http.HttpStatus;6 importorg.apache.http.NameValuePair;7 importorg.apache.http.client.entity.UrlEncodedFormEntity;8 importorg.apache.http.client.methods.CloseableHttpResponse;9 importorg.apache.http.client.methods.HttpPost;10 importorg.apache.http.impl.client.CloseableHttpClient;11 importorg.apache.http.impl.client.HttpClients;12 importorg.apache.http.message.BasicNameValuePair;13 importorg.apache.http.protocol.HTTP;14 importorg.apache.http.util.EntityUtils;15

16 importjava.io.IOException;17 importjava.io.UnsupportedEncodingException;18 importjava.util.ArrayList;19 importjava.util.HashMap;20 importjava.util.List;21 importjava.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 classSDTestDemo {34 public static voidmain(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 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 = newSDTestUtil();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 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(newUrlEncodedFormEntity(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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值