1. 程式人生 > >模擬不同國家ip的請求

模擬不同國家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<String,String> 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<String,String> 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<NameValuePair> nvps = new ArrayList<NameValuePair>();
        if(map!=null){
            for (Map.Entry<String, String> 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;
    }


}