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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.*; /** * 流量订单Demo * @ClassName FlowOrderDemo * @Description * @Date 2021/11/29 10:10 **/ public class FlowOrderDemo { /*------------------------------ 常量 ---------------------------------*/ public static String PROXY_HOST = "flow.hailiangip.com"; // 请求的ip地址 public static int HTTP_PROXY_PORT = 14223; // http代理端口 public static int SOCKS5_PROXY_PORT = 14224; // socks5代理端口 public static String DEST_URL = "http://www.baidu.com"; // 目标地址 /*------------------------------ 常量 ---------------------------------*/ /*------------------------------ 参数 ---------------------------------*/ public static String orderId = "O2103***********56207"; // 订单号 public static String pid = "-1"; // 省份id,-1表示随机 public static String cid = "-1"; // 城市id,-1表示随机 public static String sip = "0"; // 是否切换IP,0表示自动切换,1表示不能切换,默认0 public static String uid = ""; // 自定义,dik情况下,相同的UID会尽可能采用相同的IP,可以认为是同一组会话,不填表示每次请求都随机 public static String zoneId = ""; // 通道ID private static String pwd = ""; // 订单密码 /*------------------------------ 参数 ---------------------------------*/ public static void main(String[] args) { try { // 用户名 String username = ""; // password,是由以上参数组合而成的,用 '&' 符合分隔,pwd和time必传 String password = ""; // 模式,0-默认账密模式,1-通道模式。此变量只是为了代码上拼接2种使用方式的 password 参数 int mode = 0; // 默认账密模式 if (mode == 0) { username = orderId; password = password + "pwd=" + pwd + "&pid=" + pid + "&cid=" + cid + "&uid=" + uid + "&sip=" + sip; } // 通道模式 else if (mode == 1) { username = orderId; password = password + "pwd=" + pwd + "&zoneId=" + zoneId; } httpProxyWithPassRequest(DEST_URL, username, password); socks5ProxyWithPassRequest(DEST_URL, username, password); } catch (Exception e) { System.out.println(e); } } public static void httpProxyWithPassRequest(String destUrl, String username, String proxyPassword) throws IOException { CredentialsProvider provider = new BasicCredentialsProvider(); HttpHost host = new HttpHost(PROXY_HOST, HTTP_PROXY_PORT); provider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(username, 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 void socks5ProxyWithPassRequest(String destUrl, String username, String proxyPassword) throws IOException { Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, SOCKS5_PROXY_PORT)); URL url = new URL(destUrl); HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy); httpUrlConnection.setRequestMethod("GET"); httpUrlConnection.setConnectTimeout(5 * 1000); httpUrlConnection.setRequestProperty("Accept-Encoding", "gzip"); // 设置代理认证 Authenticator authenticator = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return (new PasswordAuthentication(username, proxyPassword.toCharArray())); } }; Authenticator.setDefault(authenticator); // 发起请求 httpUrlConnection.connect(); // 输出状态码 System.out.println("socks5代理请求状态码: " + httpUrlConnection.getResponseCode()); InputStream inputStream = httpUrlConnection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); String s; while ((s = reader.readLine()) != null) { sb.append(s); } // 响应内容 System.out.println("socks5代理请求响应内容: " + sb.toString()); httpUrlConnection.disconnect(); } }