本帖最后由 庭院深深深几许 于 2019-1-4 14:13 编辑
[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<String, Object> 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<NameValuePair> 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();
}
}
}
}
NameValuePair方式传参数
|