对单个站点的多个 GET 请求(规范)(java)

发布于 2024-11-27 04:50:40 字数 434 浏览 0 评论 0原文

您好,我正在尝试向单个连接发出 2 个 GET 请求。即

HttpGet get1 = new HttpGet("http://www.google.com/search?q=HelloWorld");
HttpGet get2 = new HttpGet("http://www.google.com/search?q=SecondSearch");

HttpResponse response = null;

response = client.execute(get1);
response = client.execute(get2);

我想从第二次执行中获取主体。显然这失败了,因为它说你必须先释放连接。我需要维护确切的会话 - 例如,如果我导航到第一步是登录的网站,我需要使用相同的 cookie 导航到任何后续页面。

这可能是非常简单的事情,我做错了!

Hi I am trying to make 2 GET requests to a single connection. ie

HttpGet get1 = new HttpGet("http://www.google.com/search?q=HelloWorld");
HttpGet get2 = new HttpGet("http://www.google.com/search?q=SecondSearch");

HttpResponse response = null;

response = client.execute(get1);
response = client.execute(get2);

I would like to get the body from the second execution. Obviously this fails, because it says you must release the connection first. I need to maintain the exact session - for instance, if I navigate to a site where the first step is to login, I need to navigate to any subsequent pages with the same cookie.

It's probably something incredibly simple that I am doing wrong!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-12-04 04:50:41

您需要使用 CookieStore< /a>

CookieStore cookieStore = new BasicCookieStore();

DefaultHttpClient client1 = new DefaultHttpClient();
client1.setCookieStore(cookieStore);
HttpGet httpGet1 = new HttpGet("...");
HttpResponse response1 = client1.execute(httpGet1);

DefaultHttpClient client2 = new DefaultHttpClient();
client2.setCookieStore(cookieStore);
HttpGet httpGet2 = new HttpGet("...");
HttpResponse response2 = client2.execute(httpGet2);

在上面的代码中,两个 client2 都会重复使用 client1 请求中的 cookie。

You need to use a CookieStore

CookieStore cookieStore = new BasicCookieStore();

DefaultHttpClient client1 = new DefaultHttpClient();
client1.setCookieStore(cookieStore);
HttpGet httpGet1 = new HttpGet("...");
HttpResponse response1 = client1.execute(httpGet1);

DefaultHttpClient client2 = new DefaultHttpClient();
client2.setCookieStore(cookieStore);
HttpGet httpGet2 = new HttpGet("...");
HttpResponse response2 = client2.execute(httpGet2);

In the above code, both client2 will re-use cookies from the client1 request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文