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

好的,以下是使用Apache HttpClient请求POST接口的示例代码: ```java import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/api"); // 设置请求头 httpPost.setHeader("Content-Type", "application/json"); // 设置请求体 String requestBody = "{\"name\":\"John\",\"age\":30}"; StringEntity entity = new StringEntity(requestBody, StandardCharsets.UTF_8); httpPost.setEntity(entity); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8); System.out.println("Response: " + responseString); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们创建了一个默认的HttpClient实例,然后构造了一个HttpPost请求对象,设置了请求头和请求体,最后发送请求并获取响应。你只需要将请求地址和请求体替换成你的实际接口地址和请求参数即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值