添加相关依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
配置以下restTemplate到spring容器中:
@Configuration
public class RestConfig {
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);
factory.setConnectTimeout(5000);
return new RestTemplate(factory);
}
}
请求数据
请求
请求行
请求方法
请求url
http协议版本
请求头
空行
请求体
响应
状态行
报文协议及版本
状态码及状态描述
响应头
空行
响应体
请求实体
package com.toro;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
String name;
}
请求
package com.toro;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
public class httpTest {
public static void getHttp() {
try {
RestTemplate restTemplate = new RestTemplate();
/**
* 请求url
*/
String url = "http://vs.clouddkj.com/YDS/doPost";
/**
* 请求实体
*/
Book book = new Book();
/**
* 设置请求头
*/
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", "ASP.NET_SessionId=vr2d21dftdzblg5edxlp1ytn");
//如果发送的参数数据是json数据的话,需要添加特殊的请求头
// headers.setContentType(MediaType.APPLICATION_JSON);
/**
* 请求的实体类
*/
//添加参数,因为HttpEntity里面的参数是MultiValueMap类型的,所以使用这个map集合
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("className", "CloudDot.DC.BLL.DCBLL");
map.add("methodName", "QueryDataLibList");
map.add("params", "[\"\"]");
//添加请求的实体类,这里第一个参数是要发送的参数,第二个参数是请求头里的数据
HttpEntity<Object> requestEntity = new HttpEntity<>(map, headers);
//这种方法是最通用的,事实上,它是 RestTemplate 中所有其他方法使用的底层方法。
ResponseEntity<Book> response = restTemplate.execute(
url,
HttpMethod.POST,
new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
// 操作请求头和请求体
}
},
new ResponseExtractor<ResponseEntity<Book>>() {
@Override
public ResponseEntity<Book> extractData(ClientHttpResponse response) throws IOException {
// 操作响应并返回 ResponseEntity
return null;
}
}
);
//用 HttpEntity 对其进行封装。这样,我们就可以在请求中明确设置额外的 HTTP 请求头信息。
//exchange() 方法是通用的,这意味着它可以用于任何 HTTP 方法。
// 因此,URL 后的第二个参数必须指明请求使用的方法。
//会调用execute()方法
ResponseEntity<String> response2 = restTemplate.exchange(
url,
HttpMethod.GET,
//请求实体
requestEntity,
//响应实体
String.class
);
// 获取状态码
int statusCodeValue = response.getStatusCodeValue();
// 状态行
HttpStatus statusCode = response.getStatusCode();
// 获取状态码
int statusCodeValue2 = statusCode.value();
if (statusCodeValue == HttpStatus.OK.value()) {
// 处理成功的响应
// 响应头
HttpHeaders headers1 = response.getHeaders();
// 响应体
Book body = response.getBody();
} else if (statusCodeValue == HttpStatus.NOT_FOUND.value()) {
// 处理资源未找到的响应
// ...
} else {
// 处理其他错误情况
// ...
}
} catch (Exception e) {
e.printStackTrace();
}
}
}