有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用HTTPClient启用java Cookie

我正在尝试实现一个Java代码来获取URL,如下所示: http://trendistic.indextank.com/_trending-topics-archive/2011-12-25

我编写的代码基于HTTPClient,如下所示:

    HttpClient httpclient = new DefaultHttpClient();
    // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();
    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet(urls);

    try {
        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget, localContext);
        String line = "";
        StringBuilder total = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        while ((line = rd.readLine()) != null) {
            total.append(line+"\n");
        }
        System.out.println(total);

        return (total.toString());

    } catch (Exception e) {
        e.printStackTrace();
        return(e.getMessage());
    }
}

然而,我得到的答复是:

if (tz != tzless.readCookie('local_tz')) {
    // no cookies!
    document.write("<h1>Cookies are not enabled for this site</h1>");
    document.write("<p>In order for Trendistic to work in Internet Explorer you need to enable cookies.</p>");
    document.write("<ul><li>Please enable cookies for this site and refresh this page</li><li>Or try Trendistic in a different browser such as Safari, Firefox or Chrome.</li></ul>");
} else {
    self.location.reload();
}

如何启用cookies来获取具有动态结果的HTML


共 (0) 个答案