package com.haodf.search.manager.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@Slf4j
public class HttpClientUtil {
public static ResponseEntity<String> restTemplatePost(String url, String params) {
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> requestEntity = new HttpEntity<String>(params, headers);
RestTemplate restTemplate = new RestTemplate();
//执行HTTP请求
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
}
public static String restTemplatGet(String url) {
RestTemplate restTemplate = new RestTemplate();
//执行HTTP请求
return restTemplate.getForObject(url, String.class);
}
public static void restTemplatDelete(String url) {
RestTemplate restTemplate = new RestTemplate();
//执行HTTP请求
restTemplate.delete(url);
}
/**
* java原生的http请求
* resttemplate不支持:http://localhost:8080//index/handleOneData?typeName=doctor&type=1&id=6964492271
* @param urlToRead
* @return
* @throws Exception
*/
public static String getHttpURLConnection(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
/**
* post消息体是json的请求
* @param url
* @param request
* @return
*/
public static String postJson(String url, String request){
RestTemplate restTemplate = new RestTemplate();
// Add the Jackson message converter
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
// set headers
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//请勿轻易改变此提交方式,大部分的情况下,提交方式都是表单提交
headers.setContentType(type);
HttpEntity<String> entity = new HttpEntity<>(request, headers);
// send request and parse result
ResponseEntity<String> response = restTemplate
.exchange(url, HttpMethod.POST, entity, String.class);
return response.getBody();
}
}
RestTemplate发送Http请求
未经允许不得转载:搜云库技术团队 » RestTemplate发送Http请求
相关推荐
- 第二版:互联网大厂面试题,92份 PDF,累计 3625 页!
- 蘑菇街、滴滴、淘宝、微信的组件化架构解析,附Demo和PDF
- Mybatis源码分析 - 九种设计模式总结
- MySQl性能优化,MySQl索引优化,MySQl执行计划使用实战经历
- 如何设计网址短链接生成服务,网址缩短服务,短URL生成服务
- Nginx实现负载均衡配置,分发策略
- JVM最多支持多少个线程?如何计算JVM线程数?
- 程序该如何优化?怎么做性能优化?性能优化的原则?
- SpringBoot Jar 可执行原理,源码分析SpringBoot Jar启动
- 为什么要读源码,如何阅读源码,Spring源码如何读,业务源代码如何读
- StringBuilder为什么线程不安全,StringBuilder源码分析
- JDK8 Stream 数据流效率分析,JDK8 Stream 性能如何
- 如何计算并发用户数,PV计算公式,TPS估计
- Tomcat性能调优,JVM的性能调优,总结文档
- 生产上MySQL慢查询优化实战,SQL优化实战
- SpringBoot 中 logback日志配置使用