您当前所在的位置: 首页 > 帮助中心 > 常见问题
JAVA 使用代理IP 的通用代码示例

import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.BasicCredentialsProvider;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;


import java.io.IOException;


/**

 * 流量订单Demo

 * @ClassName FlowOrderDemo

 * @Description

 * @Date 2019/10/25 15:10

 **/

public class FlowOrderDemo {

    public static String orderId = "";

    public static String secret = "";

    public static String proxyHost = "flow.hailiangip.com";

    public static int proxyPort = 14223;


    public static String createPwd(String orderId, String secret) {

        String signTemplate = "orderId={orderId}&secret={secret}&time={time}";

        String passwordTemplate = "orderId={orderId}&sign={sign}&time={time}&pid=-1&cid=-1&uid=&sip=0&nd=0";

        long time = System.currentTimeMillis() / 1000;

        String str1 = signTemplate.replace("{orderId}", orderId)

                .replace("{secret}", secret)

                .replace("{time}", String.valueOf(time));

        String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(str1).toLowerCase();

        return passwordTemplate.replace("{orderId}", orderId)

                .replace("{sign}", sign)

                .replace("{time}", String.valueOf(time));

    }


    public static void httpProxyWithPassRequest(String destUrl,String proxyHost, int proxyPort, String proxyUserName, String proxyPassword) throws IOException {

        HttpHost host = new HttpHost(proxyHost, proxyPort);

        CredentialsProvider provider = new BasicCredentialsProvider();

        provider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(proxyUserName, proxyPassword));


        CloseableHttpClient client = HttpClients.custom()

                .setDefaultCredentialsProvider(provider).build();


        HttpGet httpGet = new HttpGet(destUrl);

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(6000)

                .setSocketTimeout(6000).setProxy(host).build();


        httpGet.setConfig(requestConfig);

        HttpResponse httpResponse = client.execute(httpGet);

        System.out.println("http代理请求状态码: " + httpResponse.getStatusLine());

        System.out.println("http代理请求响应内容: " +EntityUtils.toString(httpResponse.getEntity()));


    }


    public static String destUrl = "http://api.hailiangip.com:8422/api/myIp";

    public static void main(String[] args) {

        try {

            httpProxyWithPassRequest(destUrl, proxyHost , proxyPort, "proxy", createPwd(orderId, secret));

        } catch (Exception e) {

            System.out.println(e);

        }

    }

}


  • 真实产品信息
  • 10日无条件退换货
  • 1000城市货到付款
  • 专业服务团队
在线客服
NEW
专属客服为您服务
VIP客服
3159548590
前台客服
3475802400
技术总监
941882361