遇到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(); }