java实现模拟不同国家ip的请求

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class HttpTest {
  
    public static void main(String[] args) throws IOException {

        String url = "http://clk.trkmobi.net/call/v2/ad/click";
        //data=URLEncoder.encode(data,"utf8");
        Map map = new HashMap<>();
        map.put("recommend_id","5800001");
        map.put("ads_id","47476944");
        map.put("aff_id","1004081");
        map.put("ak_id","15511");

        map.put("aff_sub","IIvIszV-aNbE_5_QPY7iKwX-WPDnDBDZMJ90Bvpq9blFR8K9bLkhigGujdlrWhmUlYbyNewj6MVtPmL3B2mbuzSAsP_l-4kKH3xBHdP_3mfRSOLS_3V5kyL1Qkxjd-dM");
        map.put("aff_sub2","2b9f8ef9-af50-4e16-973a-fe7d76cf39fb");
        map.put("aff_sub3","1d4f10d-7878-4a15-b33f-ad711d48058c");
        send(url,map,"utf-8");
    }


  

    /**
     * 执行HTTP 请求
     * @param url
     * @param map
     * @param encoding
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String send(String url, Map map, String encoding) throws IOException {
        CloseableHttpResponse response = null;
        String body = "";

        //创建httpclient对象
        CloseableHttpClient client =  HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler(0,false)).build();

        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);

        //装填参数
        List nvps = new ArrayList();
        if(map!=null){
            for (Map.Entry entry : map.entrySet()) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        //设置参数到请求对象中
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10000).setSocketTimeout(10000).build();
        httpPost.setConfig(requestConfig);
        httpPost.addHeader("x-forwarded-for","210.125.84.15");  //韩国ip
        //httpPost.addHeader("x-forwarded-for","169.235.24.133"); //美国ip
        //httpPost.addHeader("x-forwarded-for","116.21.94.96");   //中国ip
        //
        System.out.println("请求地址:"+url);
        System.out.println("请求参数:"+nvps.toString());
        //执行请求操作,并拿到结果(同步阻塞)
        response = client.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
//        log.info("响应:"+body);

        System.out.println("响应:"+JsonUtil.object2JsonString(body)+"  response:"+response.getStatusLine());
        return body;
    }


}

 

你可能感兴趣的:(java,web)