http请求:post RestTemplate方式HttpEntity参数方式
一、没啥技术含量直接看看代码就好
package cn.com.itrus.datasync.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
public class HttpUtil {
private static Logger logger = LogManager.getLogger(HttpUtil.class);
public static String httpPost(String processorUrl, String message)
{
logger.debug("开始发送消息url:"+processorUrl);
try
{
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(message, headers);
logger.debug("http请求的内容为:"+message);
String result = template.postForObject(processorUrl, formEntity, String.class);
logger.debug("http响应的内容为:"+result);
return result;
}
catch(Exception ex)
{
logger.error("发送消息异常:", ex);
return null;
}
}
}