자바 서로 다른 국가 ip 시 뮬 레이 션 요청 실현

3753 단어 자바web
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;
    }


}

좋은 웹페이지 즐겨찾기