帮助中心

java语言如何接入代码demo

MAVEN引入如下依赖包:

<dependencies>

<dependency>

<groupId>cn.hutool</groupId>

<artifactId>hutool-all</artifactId>

<version>5.3.5</version>

</dependency>

<dependency>

<groupId>com.squareup.okhttp3</groupId>

<artifactId>okhttp</artifactId>

<version>4.7.1</version>

</dependency>

</dependencies>

复制下面代码,新建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("数据资源:" + 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();

}

}

}