RestTemplate如何设置Header、如何设置代理发起请求

一、restTemplate 设置Header

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

// 声明一个header变量
HttpHeaders headers = new HttpHeaders();  
// 设置 user-agent
headers.set("user-agent","");                     
// 设置为异步请求
headers.set("X-Requested-With","XMLHttpRequest");

HttpEntity entity = new HttpEntity(headers);
        
// ResponseEntity封装了返回的数据,包括了request、body、header等
ResponseEntity<JSONObject> jsonObject = restTemplate.exchange("URL", HttpMethod.GET,entity, 
                       JSONObject.class);                
// 打印请求的获取到的数据
System.out.println(jsonObject.getBody());

 

二、设置使用代理发起请求

有时候需要爬取一些国外的网站的时候,在有 SSR 魔法上网的前提下,即使开启了全局模式或者设置了IDEA使用代理,在使用RestTemplate发起get或post请求的时候实际上并没有使用代理,会报 connection timeout 的错误。这个时候,需要进行设置restTemplate使用ssr代理

首先得有这个:

然后代码使用:

 // 设置ssr代理
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory reqfac = new SimpleClientHttpRequestFactory();
reqfac.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080)));
restTemplate.setRequestFactory(reqfac);

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设置RestTemplateheader,可以按照以下步骤进行: 1. 首先,导入需要的包: import org.springframework.http.HttpHeaders; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; 2. 创建一个HttpHeaders对象,并设置需要的header信息: HttpHeaders headers = new HttpHeaders(); headers.set("user-agent", ""); // 设置user-agent headers.set("X-Requested-With", "XMLHttpRequest"); // 设置X-Requested-With 3. 创建一个HttpEntity对象,并将headers设置为其中的参数: HttpEntity entity = new HttpEntity(headers); 4. 使用exchange方法发送请求并获取返回数据: ResponseEntity<JSONObject> response = restTemplate.exchange("URL", HttpMethod.GET, entity, JSONObject.class); 5. 通过response获取返回的数据: JSONObject jsonObject = response.getBody(); 下面是一个完整的示例代码: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("user-agent", ""); headers.set("X-Requested-With", "XMLHttpRequest"); HttpEntity entity = new HttpEntity(headers); ResponseEntity<JSONObject> response = restTemplate.exchange("URL", HttpMethod.GET, entity, JSONObject.class); JSONObject jsonObject = response.getBody(); System.out.println(jsonObject); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值