使用Java通过API接口获取代理IP的代码样例
站大爷
官方
2024-01-20
1833 浏览
温馨提示:
1. 运行环境要求 jdk >= 1.6
/**
* 使用jdk原生库调用API
*/
public class TestAPI {
private static String apiUrl = "http://www.***.com/ShortProxy/GetIP/?api=1234567890&akey=8a17ca305f683620&count=1×pan=3&type=3" //API链接
public static void main(String[] args) {
HttpRequest request = new HttpRequest();
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept-Encoding", "gzip"); //使用gzip压缩传输数据让访问更快
try{
HttpResponse response = request.sendGet(apiUrl, null, headers, null);
System.out.println(response.getCode());
System.out.println(response.getContent());
}
catch (Exception e) {
e.printStackTrace();
}
}
}