Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试获取纽约时报页面时,使用GAE的URLFACHTSERVICE返回null_Java_Google App Engine_Httprequest_Urlfetch - Fatal编程技术网

Java 尝试获取纽约时报页面时,使用GAE的URLFACHTSERVICE返回null

Java 尝试获取纽约时报页面时,使用GAE的URLFACHTSERVICE返回null,java,google-app-engine,httprequest,urlfetch,Java,Google App Engine,Httprequest,Urlfetch,我正在使用下面的代码获取《纽约时报》页面的html,不幸的是,它返回null。我曾尝试过其他网站(CNN、卫报等),效果很好。我正在使用谷歌应用程序引擎的URLFetchService 下面是代码片段。请告诉我我做错了什么 //url = https://www.nytimes.com/2017/05/02/us/politics/health-care-paul-ryan-fred-upton-congress.html private String extractFromUrl(Strin

我正在使用下面的代码获取《纽约时报》页面的html,不幸的是,它返回null。我曾尝试过其他网站(CNN、卫报等),效果很好。我正在使用谷歌应用程序引擎的URLFetchService

下面是代码片段。请告诉我我做错了什么

//url = https://www.nytimes.com/2017/05/02/us/politics/health-care-paul-ryan-fred-upton-congress.html

private String extractFromUrl(String url, boolean forced) throws java.io.IOException, org.xml.sax.SAXException,
                      de.l3s.boilerpipe.BoilerpipeProcessingException  {

    Future<HTTPResponse> urlFuture = getMultiResponse(url);

    HTTPResponse urlResponse = null;
    try {
        urlResponse = urlFuture.get(); // Returns null here
    } catch ( InterruptedException ie ) {
        ie.printStackTrace();
    } catch ( ExecutionException ee ) {
        ee.printStackTrace();
    }

    String urlResponseString = new String(urlResponse.getContent());
    return urlResponseString;
}

public Future<HTTPResponse> getMultiResponse(String website) {
    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
    URL url = null;
    try {
        url = new URL(website);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    FetchOptions fetchOptions = FetchOptions.Builder.followRedirects();
    HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
    Future<HTTPResponse> futureResponse = fetcher.fetchAsync(request);
    return futureResponse;
}

查看curl的详细输出,您可以看到该网站试图设置cookie,并在cookie不被接受的情况下重定向您

《纽约时报》似乎会在你放弃之前重定向你7次-

$ curl --verbose -L "https://www.nytimes.com/2017/05/02/us/politics/health-care-paul-ryan-fred-upton-congress.html" 2>&1 | grep 303 | wc -l
7
似乎UrlFetch的最大重定向数为5[0]

为了成功抓取www.nytimes.com,您必须禁用以下重定向并自行处理cookie逻辑。这里有一些启示[1]和[2]

[0]

[1]


[2]

谢谢。我会查那些建议。最后我要查一下你的建议,让我的代码正常工作。在第一次请求时设置cookie之后,NYT会将您重定向到另一个页面,在那里他们会设置两个以上的cookie。然后他们将您重定向回原来的页面,在那里使用了所有三个cookie(一个来自第一个请求,两个来自重定向的第二个请求)。@BlueChips23真棒!这种行为很可能会再次发生变化,所以请确保您有一些良好的日志记录和错误处理,以防这不是一次性的。
$ curl --verbose -L "https://www.nytimes.com/2017/05/02/us/politics/health-care-paul-ryan-fred-upton-congress.html" 2>&1 | grep 303 | wc -l
7