需要jar包apache-httpclient5.4.1
package com.lvhe.brokercenter.assessment;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.entity.StringEntity;
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 org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.lvhe.brokercenter.client.domain.backstageapp.UserCardInfoDO;
public class TestService {
@Test
public void test1(){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8080/myWallet/bindingBankCards");
UserCardInfoDO userCardInfoDO = new UserCardInfoDO();
userCardInfoDO.setBankPhone("18701320082");
userCardInfoDO.setCardNum("655879584265456887");
userCardInfoDO.setIdNum("131081199008041827");
userCardInfoDO.setOpeningBank("中信银行");
userCardInfoDO.setOpeningBranchBank("东湖渠支行");
userCardInfoDO.setUserName("樊丽丽");
List nvps = new ArrayList ();
nvps.add(new BasicNameValuePair("bankPhone", "18701320082"));
nvps.add(new BasicNameValuePair("cardNum", "655879584265456887"));
nvps.add(new BasicNameValuePair("idNum", "131081199008041827"));
nvps.add(new BasicNameValuePair("openingBank", "中信银行"));
nvps.add(new BasicNameValuePair("branchBank", "东湖渠支行"));
nvps.add(new BasicNameValuePair("userName", "樊丽丽"));
String json = JSON.toJSONString(userCardInfoDO);
// StringEntity stringEntity = new StringEntity(json, "UTF-8");
// stringEntity.setContentType("application/x-www-form-urlencoded");
// httppost.setEntity(stringEntity);
try {
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity myEntity = response.getEntity();
System.out.println(myEntity.getContentType());
System.out.println(myEntity.getContentLength());
String resString = EntityUtils.toString(myEntity);
String result = JSON.toJSONString(resString);
System.out.println("result" + result);
// 使用返回的字符串直接构造一个JSONObject
// JSONObject jsonobj = new JSONObject(resString);
// System.out.println(jsonobj.toString());
// // 获取返回对象中"resultSize的值"
// int resutltSize = jsonobj.getInt("resultSize");
// System.out.println("Search Results Size is: "+ resutltSize);
// // 获取"clients"的值,它是一个JSONArray
// JSONArray jsonarray = jsonobj.getJSONArray("clients");
// System.out.println(jsonarray.toString());
}catch(Exception e){
}
}
}