public class Tp {
public static void main(String[] args) {
/*// 如果不设置,只要代理IP和代理端口正确,此项不设置也可以
System.getProperties().setProperty("http.proxyHost", "10.22.40.32");
System.getProperties().setProperty("http.proxyPort", "8080");
sendGet( "http://www.****.html", "");*/
for(int i =0;i<100;i++){
try {
String post4Json =getPost4Json("http://www.******.html", getIp());
System.out.println(post4Json);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(i);
}
}
public static StringgetIp(){
int x=(int)(Math.random()*200);
int y=(int)(Math.random()*200);
return "192.168."+String.valueOf(x)+"."+y;
}
public static StringgetPost4Json(String url, String ip)throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpPost =new HttpGet(url);
/* 设置超时 */
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();
httpPost.setConfig(defaultRequestConfig);
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
//httpPost.setEntity(new StringEntity(json, "UTF-8"));
httpPost.addHeader("x-forwarded-for",ip);
CloseableHttpResponse response =null;
String result =null;
try {
response = httpClient.execute(httpPost);
HttpEntity entity = (HttpEntity) response.getEntity();
result = EntityUtils.toString((org.apache.http.HttpEntity) entity, "UTF-8");
}catch (Exception e) {
throw e;
}finally {
if (response !=null) response.close();
httpClient.close();
}
return result;
}
}