• http请求提交cookie


    package leadsServerTest;


    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.impl.cookie.BasicClientCookie;
    import org.apache.http.util.EntityUtils;
    import org.junit.Test;

    import java.io.IOException;
    import java.text.MessageFormat;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    public class performanceTest {

    @Test
    public void batchPost() throws IOException {
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    for (int i = 100; i < 100; i++) {
    int finalI = i;
    executorService.execute(new Runnable() {
    @Override
    public void run() {
    try {
    //6e296cdf-891b-40c7-bbbe-7b5f64e9893d
    //b3524207-7505-42af-a8d8-1e8da6e9ff70
    String showId="744ddbbd-4686-4c2b-874a-f63534c139b4";
    /* if(finalI%2==0){
    showId="b3524207-7505-42af-a8d8-1e8da6e9ff70";
    }*/
    getAndPostData("18300000" + String.valueOf(finalI),showId);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    });
    }

    System.in.read();


    }

    void getAndPostData(String phoneNo,String showId) throws IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("dealerAdvShowId", showId);//"b3524207-7505-42af-a8d8-1e8da6e9ff70");
    cookie.setDomain(".dealeradv.autohome.com.cn");
    cookie.setPath("/");

    cookieStore.addCookie(cookie);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

    final HttpGet request = new HttpGet("https://dealeradv.autohome.com.cn/Leads/getCreativeInfo");
    HttpResponse response = client.execute(request);
    if (!outPutResponseResult(response, phoneNo)) {
    // return;
    //System.out.println("get Exception :"+phoneNo);
    System.out.println(phoneNo+"------------------------------ get Exception :"+EntityUtils.toString(response.getEntity()));
    return;
    }

    System.out.println("request get phoneNo :" +phoneNo);

    //提交数据
    String url = MessageFormat.format("https://dealeradv.autohome.com" +
    ".cn/Leads/index?car_series={0}&car_spec={1}&order_name={2}&order_phone_number={3}&city" +
    "={4}&order_ip={5}", "3554", "27769", "xu", phoneNo, "421100", "127.0.0.1");
    HttpGet requestPostData = new HttpGet(url);
    HttpResponse responsePost = client.execute(requestPostData);
    if (!outPutResponseResult(responsePost, phoneNo)){
    System.out.println(phoneNo+"------------------------------ post Exception :"+EntityUtils.toString(responsePost.getEntity()));
    }

    }

    boolean outPutResponseResult(HttpResponse response, String phoneNo) throws IOException {
    if (response.getStatusLine().getStatusCode() == 200) {
    return true;
    /*Protocol protocol = JSON.parseObject(EntityUtils.toString(response.getEntity()), Protocol.class);
    if(protocol.getReturncode()==0){
    System.out.println(phoneNo+",request successfully."+ EntityUtils.toString(response.getEntity()));
    return true;
    }
    else{
    System.out.println(phoneNo+",request faild."+ EntityUtils.toString(response.getEntity()));
    return false;
    }*/
    } else {
    //System.out.println(phoneNo + ", exception.");
    return false;
    }
    }
    }
  • 相关阅读:
    理解Android系统的进程间通信原理(一)----RPC中的代理模式
    Android系列之Android 命令行手动编译打包详解
    CodeForces 681B Economy Game (暴力)
    CodeForces 681A A Good Contest (水题)
    UVa 1614 Hell on the Markets (贪心+推理)
    UVa 247 Calling Circles (DFS+Floyd)
    UVa 1151 Buy or Build (最小生成树+二进制法暴力求解)
    UVa 1395 Slim Span (最小生成树)
    HDU 1071 The area (数学定积分)
    HDU 1286 找新朋友 (欧拉phi函数打表)
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/6202879.html
Copyright © 2020-2023  润新知