Apache HttpClient 4.5.5 - GET请求后响应中的错误标头

时间:2018-02-22 05:30:09

标签: java apache-httpclient-4.x

当我在使用Apache HttpClient并在响应中加载页面后通过GET请求加载网页时,我的标题与我在浏览器中加载同一页面时的标题不同。以下是该页面的示例:http://empoweredfoundation.org/wp-login.php?action=register

在浏览器中,我有以下标题:

 for (*i = 0; *i<*n; (*i)++)
    {
        cin >> *(arr + *i);

    }

当我在我的应用程序中使用HttpClient时,我会回复这些标题:

Status code: 302
Content-Type: text/html; charset=UTF-8
X-Port: port_10210
X-Cacheable: YES:Forced
Location: http://empoweredfoundation.org/register/
Content-Encoding: gzip
Transfer-Encoding: chunked
Date: Thu, 22 Feb 2018 04:04:35 GMT
Age: 0
Vary: User-Agent
X-Cache: uncached
X-Cache-Hit: MISS
X-Backend: all_requests

所以,我应该有一个302状态代码,但我有200个。而且我也看到其他标题与浏览器中的标题不同。我无法弄清楚为什么以及如何解决这个问题。

以下是代码:

Status code: 200
Content-Type: text/html; charset=UTF-8
X-Port: port_10210
X-Cacheable: YES:Forced
Transfer-Encoding: chunked
Date: Thu, 22 Feb 2018 04:44:58 GMT
Age: 28224
Vary: Accept-Encoding, User-Agent
X-Cache: cached
X-Cache-Hit: HIT
X-Backend: all_requests
Server: nginx/1.12.1
Date: Thu, 22 Feb 2018 04:45:24 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.45

我也尝试过CloseableHttpClient,结果相同。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,此解决方案有效:https://memorynotfound.com/apache-httpclient-redirect-handling-example/

我仍然有200个状态代码,而不是302.但现在我可以处理302重定向(即使response.getStatusLine()显示200)。

以下是文章中的代码:

package com.memorynotfound.httpclient;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * This example demonstrates the use of {@link HttpGet} request method.
 * and handling redirect strategy with {@link LaxRedirectStrategy}
 */
public class HttpClientRedirectHandlingExample {

    public static void main(String... args) throws IOException, URISyntaxException {

        CloseableHttpClient httpclient = HttpClients.custom()
                .setRedirectStrategy(new LaxRedirectStrategy())
                .build();

        try {
            HttpClientContext context = HttpClientContext.create();
            HttpGet httpGet = new HttpGet("http://httpbin.org/redirect/3");
            System.out.println("Executing request " + httpGet.getRequestLine());
            System.out.println("----------------------------------------");

            httpclient.execute(httpGet, context);
            HttpHost target = context.getTargetHost();
            List<URI> redirectLocations = context.getRedirectLocations();
            URI location = URIUtils.resolve(httpGet.getURI(), target, redirectLocations);
            System.out.println("Final HTTP location: " + location.toASCIIString());

        } finally {
            httpclient.close();
        }
    }
}

在创建HttpClient类对象时,我也添加了builder.setRedirectStrategy(new LaxRedirectStrategy());

如果你知道任何获得正确状态代码的解决方案(应该是302),请告诉我。

相关问题