java操作http请求的三种方式
一、HttpClient
步骤:
1.获取一个Http客户端
CloseableHttpClient httpClient=HttpClients.createDefault();
2.创建一个请求
HttpGet httpGet = new HttpGet("http://sign.hrit.haier.net/api/get");//get请求
HttpPost httpPost = new HttpPost("http://sign.hrit.haier.net/api/get"); //post请求
HttpPut httpPut = new HttpPut("http://sign.hrit.haier.net/api/get"); //put请求
HttpDelete httpDelete = new HttpDelete("http://sign.hrit.haier.net/api/get"); //delete请求
......
3.设置请求头(不需要可忽略)
//设置请求头请求体的格式为JSON
httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
//在请求头设置认证token
httpPost.setHeader("Authorization","Basic " + Base64.getEncoder().encodeToString("username:password".getBytes()));
4.设置请求参数
//设置请求参数
HttpEntity httpEntity = new StringEntity(JSON.toJSONString(null), ContentType.APPLICATION_JSON);
//将参数放入请求
httpPost.setEntity(httpEntity);
5.执行请求并获取响应
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
6.关闭连接
response.close();
httpClient.close();
例如
public void testGetWithRest(){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://sign.hrit.haier.net/api/get");
HttpPost httpPost = new HttpPost("http://sign.hrit.haier.net/api/get");
HttpPut httpPut = new HttpPut("http://sign.hrit.haier.net/api/get");
HttpDelete httpDelete = new HttpDelete("http://sign.hrit.haier.net/api/get");
httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
httpPost.setHeader("Authorization","Basic " + Base64.getEncoder().encodeToString("username:password".getBytes()));
CloseableHttpResponse response =null;
try {
HttpEntity httpEntity = new StringEntity(JSON.toJSONString(null), ContentType.APPLICATION_JSON);
httpPost.setEntity(httpEntity);
long time = System.currentTimeMillis();
response = httpClient.execute(httpPost);
System.out.println("延迟时间:" + (System.currentTimeMillis() - time));
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity()));
response.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (response !=null){
try {
response.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (httpClient!=null){
try {
httpClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
二、RestTemplate
步骤:
1.获取一个Http客户端或注入一个RestTemplate
RestTemplate template = new RestTemplate();
2.创建一个请求头,并设置参数
//创建一个请求头
HttpHeaders headers = new HttpHeaders();
//设置请求头的参数类型为JSON
headers.setContentType(MediaType.APPLICATION_JSON);
//设置请求头的权限认证参数
headers.setBasicAuth("");
3.使用参数和请求头创建一个HttpEntity
HttpEntity<Object> http = new HttpEntity<>(new MdmCustomer(),headers);
4.发送请求并获取返回值
ResponseEntity<String> http1 = template.postForEntity("http://localhost:8080/dept", http, String.class);
System.out.println(http1.getStatusCode().value());
System.out.println(http1.getBody());
三、OpenFeign
@FeignClient(name = "cv" ,url = "http://localhost:8080/dept")
public interface CustomerVendorRemote {
@PostMapping(value = "/aa",produces = MediaType.APPLICATION_JSON_VALUE,headers="Authorization=Basic ${third.erp.authorization}")
String queryDataOnTime(@RequestBody Dept dept);
}
url:指定的http的url地址
headers:设置请求的请求头信息
produces:设置响应类型为JSON
@ReuestBody:设置请求参数为JSON