首页
静态代理IP
代理IP搭建
云服务器
软件下载
推广合作
帮助文档
协议声明
登录
注册
首页
静态代理IP
代理IP搭建
云服务器
软件下载
推广合作
帮助文档
协议声明
登录
注册
新用户注册,免费领取1000IP
帮助文档中心
使用教程
API文档
SDK示例
IP资讯
如果有任何问题,请联系我们的客服,会有专人为您服务解答。希望九零科技的产品服务能带给您安全便利!
热门搜索:
住宅ip
代理IP搭建
Python示例
首页
技术文档
SDK
文章详情
JAVA配置代理请求——示例代码
2025-03-07 17:42:59
Admin
26 阅读
MAVEN引入如下依赖包: cn.hutool hutool-all 5.3.5 com.squareup.okhttp3 okhttp 4.7.1 复制下面代码,新建Package:com.demo --> 新建Java类:Main package com.demo; import cn.hutool.http.HttpUtil; import okhttp3.*; import java.io.*; import java.net.InetSocketAddress; import java.net.Proxy; import java.util.Objects; import java.util.concurrent.TimeUnit; public class Main { // 需要请求的目标网址 private static final String TARGET_URL = "目标网址"; // 提取链接 private static final String API_URL = "提取链接"; public static void main(String[] args) { String result = HttpUtil.get(API_URL); System.out.println("从接口获取回来:" + result.strip()); String proxyIp = result.strip(); try { Proxies(TARGET_URL, proxyIp); Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } public static void Proxies(String urlString, String ipPort) { try { Response response = null; try { System.out.println("当前ip:" + ipPort); String ip = ipPort.split(":")[0]; int port = Integer.parseInt(ipPort.split(":")[1]); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port)); OkHttpClient httpClient = new OkHttpClient().newBuilder().proxy(proxy).connectTimeout(10, TimeUnit.SECONDS) .build(); Request request = null; request = (new Request.Builder().url(urlString).build()); response = httpClient.newCall(request).execute(); System.out.println("输出结果2:" + Objects.requireNonNull(response.body()).string()); } catch (IOException e) { System.out.println("错误提示1"); e.printStackTrace(); } } catch (Exception e) { System.out.println("错误提示2"); e.printStackTrace(); } } } 或者 测试2: import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.util.Base64; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ProxyExample { public static void main(String[] args) throws IOException { String url = "https://ip.tool.lu"; String proxyIp = "代理ip"; int proxyPort = 端口; String proxyAuth = “user:pass”; //这里是实际的代理账号和密码 没用自行删除 String encodedProxyAuth = Base64.getEncoder().encodeToString(proxyAuth.getBytes()); // Set proxy properties System.setProperty("http.proxySet", "true"); System.setProperty("http.proxyHost", proxyIp); System.setProperty("http.proxyPort", String.valueOf(proxyPort)); System.setProperty("https.proxyHost", proxyIp); System.setProperty("https.proxyPort", String.valueOf(proxyPort)); // Create URL object URL urlObj = new URL(url); // Open connection HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(Proxy.NO_PROXY); // Set headers conn.setRequestProperty("User-Agent", "Mozilla/5.0"); // Set proxy authorization header conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedProxyAuth); // Send GET request conn.setRequestMethod("GET"); // Read response BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Print response System.out.println(response.toString()); // Get response time long responseTime = conn.getHeaderFieldDate("Date", 0) - conn.getHeaderFieldDate("Date", 0); System.out.println(responseTime); } }
上一篇:python配置代理请求——示例代码
下一篇:C#配置代理请求——示例代码