http请求提交cookie
Posted xuxu_dragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
}
以上是关于http请求提交cookie的主要内容,如果未能解决你的问题,请参考以下文章
(转)如何实现同一浏览器多个标签页之间的通信——cookie+setInterval
10.对于HTTP的POST请求方式,用户提交的表单数据位于HTTP请求的哪一部分? b a) 请求头中 …………………