SocketTimeoutException - GAE

时间:2015-02-10 21:04:01

标签: java google-app-engine htmlunit

这是我应该创建WebClient实例的完整代码,但它继续抛出SocketTimeoutException

@Override
protected int doHandle(Request request, Response response) {

    HttpServletRequest servletRequest = ServletUtils.getRequest(request);
    HttpServletResponse servletResponse = ServletUtils.getResponse(response);

    String query = servletRequest.getQueryString();
    // Only process calls to the main HTML page, and the empty one if desired
    final String requestURI = servletRequest.getRequestURI();
    if(query == null || !query.contains(ESCAPED_FRAGMENT_FORMAT1)){
        super.doHandle(request, response);
    } else {
        String reqUrl = servletRequest.getRequestURL().toString();
        String queryString = servletRequest.getQueryString();
        if (queryString != null) {
            reqUrl += "?" + queryString;
        }
        try {
            StringBuilder pageNameSb = new StringBuilder("http://");
            pageNameSb.append(servletRequest.getServerName());
            if (servletRequest.getServerPort() != 0) {
                pageNameSb.append(":");
                pageNameSb.append(servletRequest.getServerPort());
            }
            pageNameSb.append(requestURI);
            queryString = rewriteQueryString(queryString);
            pageNameSb.append(queryString);
            String pageName = pageNameSb.toString();
            LOG.info( "Crawl filter encountered escaped fragment, will open: " + pageName );
            // Setup the headless browser
            WebClient webClient = new WebClient();
            webClient.setWebConnection(new UrlFetchWebConnection(webClient));
            webClient.setThrowExceptionOnScriptError(false);
            webClient.setJavaScriptEnabled(true);
            HtmlPage page = webClient.getPage(reqUrl);
            webClient.setTimeout(GAE_PUMP_TIME);
            PrintWriter out = servletResponse.getWriter();
            out.print(page.asXml());
            webClient.closeAllWindows();
            out.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return CONTINUE;
    }
    return STOP;
}

如何修复SocketTimeoutException,因为在变量pageName下访问生成的URL工作正常。所以我可以假设我的代码没有完全正常工作。

0 个答案:

没有答案