代码示例 私密代码示例 如何通过API获取代理IP(java)
如何通过API获取代理IP(java)
作者: 山水代理
发布时间: 2022-05-31 10:16:00
阅读量: 1019 人次

相关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();
		}
	}
		

企业微信

客服在线时间:9:00~18:00

133-5988-7911

Copyright© 2022-2023 祈美科技(牡丹江)有限公司 黑ICP备2022000763号-1 beian 黑公网安备 23100002000084号

山水代理仅提供代理IP服务,用户使用山水代理从事的任何行为均不代表山水代理的意志和观点,与山水代理的立场无关。

严禁用户使用山水代理从事任何违法犯罪行为。产生的相关责任用户自负,对此山水代理不承担任何法律责任。官网上所有内容的最终解释权归本公司所有。

企微客服
山水代理微信客服 客服二维码 扫一扫添加
联系客服
山水代理客服电话 133-5988-7911