首页 > 代码库 > http请求提交cookie

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 = http://www.mamicode.com/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;
}
}
}

http请求提交cookie