spring 封装了模拟http请求发送的调用:
工具类:
package com.tencent.sendmail.utils;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class HttpClient {
/**
* 向目的URL发送post请求
* @param url 目的url
* @param params 发送的参数
* @return AdToutiaoJsonTokenData
*/
public static String sendPostRequest(String url, MultiValueMap<String, String> params){
RestTemplate client = new RestTemplate();
//新建Http头,add方法可以添加参数
HttpHeaders headers = new HttpHeaders();
//设置请求发送方式
HttpMethod method = HttpMethod.POST;
// 以表单的方式提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//将请求头部和参数合成一个请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);