java http请求设置代理

比如对接国外平台的api  如facebook的广告api,google广告api

国内网络访问不了,需要http代理才能访问,代码中就需要用到这个

package com.giikin.ads.fb.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
//@PropertySource("proxy")
@ConfigurationProperties(prefix = "proxy")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProxySetting {
    private String proxySet;
    private String proxyHost;
    private String proxyPort;

    public String getProxySet() {
        return proxySet;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public String getProxyPort() {
        return proxyPort;
    }

    //fanqiang
    public void proxy() {
        if (proxySet.equals("true")) {
            System.out.println("proxy,run ");
            System.setProperty("proxySet", proxySet);
            System.setProperty("proxyHost", proxyHost);
            System.setProperty("proxyPort", proxyPort);
            System.setProperty("https.proxyHost", proxyHost);
            System.setProperty("https.proxyPort", proxyPort);
        }

//        System.setProperty("proxySet","true");
//        System.setProperty("socksProxyHost","47.93.244.25");
//        System.setProperty("socksProxyPort","17466");
    }

  //我用的是这个方法
public static void proxyStatic() { System.setProperty("proxySet", "true"); // System.setProperty("socksProxyHost", "39.102.39.124"); // System.setProperty("socksProxyPort", "17468"); System.setProperty("proxyHost", "192.168.4.2"); System.setProperty("proxyPort", "10809"); // System.setProperty("https.proxyHost","127.0.0.1"); // System.setProperty("https.proxyPort","1080"); // -Dhttps.proxyHost and -Dhttps.proxyPort } }

 

posted @ 2022-04-25 15:30  知行IT讲堂  阅读(1578)  评论(0编辑  收藏  举报