如何通过API获取代理IP(java)
发布时间: 2022-05-31 10:16:00
阅读量: 1139 人次
相关API
代码中所涉及的API可在【API接入指南】中查看。
参考代码
public class Main {
// 实例ID(后台可查)
private static String appKey = "appKey";
// 实例密码(后台可查)
private static String appSecret = "appSecret";
// API接口(API接入指南可查)
private static String api = "https://api.sshttp.com/getIp?appKey=%s&appSecret=%s&cnt=1&wt=text";
private static int timeout = 10000;
private static HttpClientBuilder clientBuilder = null;
// 获取代理IP
private static HttpHost getProxy() throws IOException {
CloseableHttpResponse httpResp = null;
try {
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeout)
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.setExpectContinueEnabled(false)
.setProxy(null)
.build();
HttpClientContext localContext = HttpClientContext.create();
HttpGet httpReq = new HttpGet(String.format(api, appKey, appSecret));
httpResp = clientBuilder
.setDefaultRequestConfig(requestConfig)
.build().execute(httpReq, localContext);
if (httpResp.getStatusLine().getStatusCode() == 200) {
String content = IOUtils.toString(httpResp.getEntity().getContent(), "UTF-8");
System.out.println("API response: " + content);
if (!StringUtils.isEmpty(content)) {
String[] splits = content.split(":");
if (splits.length == 2) {
return new HttpHost(splits[0], Integer.parseInt(splits[1]));
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
httpResp.close();
}
return null;
}
public static void main(String[] args) {
// 获取代理
getProxy();
}
}


黑公网安备 23100002000084号