httpclient的post测试接口demo

需要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){
  
 }
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值