java namevaluepair_NameValuePair方式传参数

博客展示了使用Java的HttpClient发送POST请求的示例。介绍了发送请求、接收响应的步骤,包括创建HttpClient对象、请求方法实例,添加请求参数,发送请求,获取响应内容等,还给出了具体代码实现,涉及NameValuePair存放参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[PHP] 纯文本查看 复制代码package com.souche.lease.finance.product;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import org.apache.http.HttpStatus;

import org.apache.http.NameValuePair;

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.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

* Created by hunt on 2017/6/26.

* 使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

* 1. 创建HttpClient对象。

* 2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

* 3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

* 4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

* 5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

* 6. 释放连接。无论执行方法是否成功,都必须释放连接

*/

public class SDTestDemo {

public static void main(String[] args) {

String licenseNo = "浙A588AX";

String token = "7cc2bd72eb1e4522804dca3b88e8644d";

String city = "330100";

String timestamp = Long.toString(System.currentTimeMillis());

String sign;

Map mapParam = new HashMap<>();

mapParam.put("licenseNo", licenseNo);

mapParam.put("token", token);

mapParam.put("city", city);

mapParam.put("timestamp", timestamp);

SDTestUtil testUtil = new SDTestUtil();

sign = testUtil.MD5(testUtil.sort(mapParam));

mapParam.put("sign", sign);

/**

* 定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型),

* 这个代码用于Java像url发送Post请求。在发送post请求时用该list来存放参数。

*/

List urlParameters = new ArrayList<>();

urlParameters.add(new BasicNameValuePair("licenseNo", licenseNo));

urlParameters.add(new BasicNameValuePair("token", token));

urlParameters.add(new BasicNameValuePair("city", city));

urlParameters.add(new BasicNameValuePair("timestamp", timestamp));

urlParameters.add(new BasicNameValuePair("sign", sign));

CloseableHttpClient httpclient = HttpClients.createDefault();

CloseableHttpResponse response = null;

HttpPost post = new HttpPost("http://101.231.154.154:8047/v4.0/renewal");

try {

post.setEntity(new UrlEncodedFormEntity(urlParameters, HTTP.UTF_8));

try {

response = httpclient.execute(post);

// 判断网络连接状态码是否正常(0--200都数正常)

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String content = EntityUtils.toString(response.getEntity(), "UTF-8");

JSONObject jsonObject = JSON.parseObject(content);

System.out.println("报价返回内容是:" + jsonObject.toString());

if ("SUCCESS".equals(jsonObject.getString("code"))) {

System.out.println("成功,系统处理正常");

}

}

EntityUtils.consume(response.getEntity());//完全消耗

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (null != response) response.close();

} catch (IOException e) {

e.printStackTrace();

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} finally {

//释放链接

try {

httpclient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值