qqqrere 2023-11-01 22:00 采纳率: 0%
浏览 34
已结题

mac电脑开启代理后restTemplate访问不到一直503

mac电脑在电脑开启代理后 在springBoot项目中使用restTemplate发送http请求 一直不通报错503 哪位mac程序员遇到过啊 m2处理器

  • 写回答

18条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-11-01 22:00
    关注
    获得0.30元问题酬金

    引自强大而免费的微信小程序:皆我百晓生

    在Mac电脑上开启代理后,可能会导致网络请求被代理拦截。这种情况下,可以尝试手动配置代理信息到RestTemplate中。

    下面是一个示例代码,展示如何配置代理信息到RestTemplate中:

    import org.springframework.http.client.ClientHttpRequestFactory;
    import org.springframework.http.client.SimpleClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    public class Main {
        public static void main(String[] args) {
            // 配置代理信息
            System.setProperty("http.proxyHost", "your-proxy-host");
            System.setProperty("http.proxyPort", "your-proxy-port");
    
            // 创建ClientHttpRequestFactory,使用代理
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("your-proxy-host", your-proxy-port));
            requestFactory.setProxy(proxy);
    
            // 创建RestTemplate,并设置ClientHttpRequestFactory
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.setRequestFactory(requestFactory);
    
            // 使用RestTemplate发送请求
            String url = "http://your-url";
            String response = restTemplate.getForObject(url, String.class);
            System.out.println(response);
        }
    }
    

    在上述代码中,你需要将your-proxy-host替换为你的代理主机名,your-proxy-port替换为你的代理端口号,your-url替换为你要发送请求的URL。

    通过以上步骤,你就可以在Mac电脑上使用代理访问RestTemplate了。如果问题仍然存在,请确认代理配置是否正确,并检查代理服务器是否正常工作。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 11月9日
  • 创建了问题 11月1日