spring boot 调用一个.htm接口

遇到springboot调用一个老接口..do形式的。进行post请求body中传参,.do接口使用HttpServletRequest request的request.getParameter("telephone");获取不到参数的问题

找到一个可用的方式

import net.sf.json.JSONObject;
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.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.*;
String responseContent="";
StringBuffer sf = new StringBuffer();
sf.append("http://请求的路径");

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(sf.toString());
List<NameValuePair> params = new ArrayList<>();
//将传递的参数这样设置
params.add(new BasicNameValuePair("username",(String)map.get("username")));
params.add(new BasicNameValuePair("telephone",(String)map.get("telephone")));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
httpPost.setEntity(entity);
try(CloseableHttpResponse response = httpClient.execute(httpPost)) {
    int statusCode =response.getStatusLine().getStatusCode();
    System.out.println("Status Code:"+ statusCode);
    org.apache.http.HttpEntity responseEntity=  response.getEntity();
    log.info("==============responseEntity========"+responseEntity);
    if(responseEntity != null) {
         responseContent = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
        System.out.println("Response Content:" + responseContent);
    }
    EntityUtils.consume((org.apache.http.HttpEntity) responseEntity);
} finally {
    httpClient.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值