使用 Java org.apache.http.client 获取重定向 url
我需要帮助来弄清楚如何在向服务器发布帖子后获取重定向。 首先,我需要执行 get 从服务器获取一些 cookie。然后我使用 cookie 和其他参数执行发布。然后服务器以 302 重定向进行应答。如何获取该重定向的 url?
代码如下:
HttpGet get = new HttpGet(urlOne);
try {
//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();
// Creating a local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);
HttpResponse response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println("------------------GET----------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// Print out cookies obtained from server
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
if (entity != null) {
entity.consumeContent();
}
System.out.println("------------------GET-END---------------------");
// Create a new post
HttpPost post = new HttpPost(urlTwo);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Add params
HttpParams params = new BasicHttpParams();
params.setParameter("action", "search");
params.setParameter("word", "hello");
post.setParams(params);
//Execute
HttpResponse response2 = httpClient.execute(post, localContext);
I need help with figuring out how to get hold of the redirect after I make a post to the server.
First, I need to do a get to obtain some cookies from the server. Then I perform a post with the cookies and additional parameters. The server then answers with a 302 redirect. How do I get the url for that redirect?
Code looks like follows:
HttpGet get = new HttpGet(urlOne);
try {
//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();
// Creating a local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);
HttpResponse response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println("------------------GET----------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// Print out cookies obtained from server
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}
if (entity != null) {
entity.consumeContent();
}
System.out.println("------------------GET-END---------------------");
// Create a new post
HttpPost post = new HttpPost(urlTwo);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Add params
HttpParams params = new BasicHttpParams();
params.setParameter("action", "search");
params.setParameter("word", "hello");
post.setParams(params);
//Execute
HttpResponse response2 = httpClient.execute(post, localContext);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅我对此问题的回答,
HttpClient 4 - 如何捕获最后一个重定向 URL
See my answer to this question,
HttpClient 4 - how to capture last redirect URL
我假设您希望自动化浏览器操作并维护会话,以便您也可以访问那些需要维护会话的页面。
我不知道如何通过 org.apache.http.client API 做到这一点。如果您不限于使用 org.apache.http.client API 并且可以使用其他 API,那么您可以使用 HtmlUnit API 否则你可以忽略答案的其余部分。
通过 HtmlUnit 维护会话并自动化浏览器操作可以如下完成:
正如您所看到的,上面的代码是如何实现的自动化浏览器操作以及它如何自动维护会话。我们不需要手动处理 cookie 或 URLReDirect...
I am assuming that you want to automate browser operations and maintain session so that you can access those pages too which need session to be maintained.
I don't know how to this through org.apache.http.client API. If you are not restricted to use org.apache.http.client API and can use other API then you can use HtmlUnit API otherwise you can ignore the rest of the answer.
Maintaining sessions and automating browser operations through HtmlUnit can be done as follows:
As you can see that how the above code automate browser operations and how it maintains session automatically. We don't need to handle cookies or URLReDirect manually...
我用Java想到了一个简单的方法。步骤如下:
HttpURLConnection.setFollowRedirects( true );
// 默认情况下应为 true 通过getHeadersFields()
;getUrl()
来获取重定向的 URL;还有另一种方法可以使用 HTTP 标头中的 Location 字段来获取它,但有时我们无法获取标头中的 Location 字段。至少对我来说没有用。但是上面的方法,肯定是有效的。
There is a simple way I came about in Java. Following are the steps:
HttpURLConnection.setFollowRedirects( true );
// this should be true by defaultgetHeadersFields()
over your HttpUrlConnection object;getUrl()
over the above HttpUrlConnection object;There is also another way of getting it using the Location field in HTTP Headers, but sometimes we do not get the Location field in headers. It did not work for me at least. But the above method, it works for sure.
它位于
位置
标题。It's available in the
location
header.