www.zhblog.net

Spring RestTemplate get请求设置headers user-agent

在Spring中使用RestTemplate发送Get请求,需要设置 user-agent。

例子:

public void requestGet() {

    String url = "https://www.zhblog.net/go/java/tutorial/springboot-build-project?t=641";

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();

    headers.set("user-agent", "Chrome/83.0.4103.116");

    HttpEntity<String> httpEntity = new HttpEntity<>(headers);

    ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);



    System.out.println(responseEntity.getStatusCode());

    System.out.println(responseEntity.getHeaders());

    System.out.println(responseEntity.getBody());

}



 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情