0

我有一个(struts 2)动作,它被称为重定向到一个jsp。我正在尝试使用 HttpClient 将此 jsp 保存为 html。除了一件事之外,这很好用,服务器jsessionid=RandomText会在 html 中存在另一个动作的地方追加。

如何从最终的 html 中删除 jsessionid ?

来自呈现的 html 的示例:

<a href='/wah/destinationSEO.action;jsessionid=123ASDGA123?idDestination=22'>

这就是我将 jsp 保存到 html 的方式:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet("http://127.0.0.1:8080/wah/destinationSEO.action?idDestination=8");

HttpResponse response = httpClient.execute(getRequest);

if (response.getStatusLine().getStatusCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());
}
httpClient.getConnectionManager().shutdown();

String content = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));

String path="D:/wahSVN/trunk/src/main/webapp/seo.html";
File html = new File(path);
html.createNewFile();
Files.append(content, html, Charsets.UTF_8);
4

2 回答 2

1

按照 amicngh 的建议,正则表达式成功了!我希望我可以在客户端/请求中指定一些东西,但是好吧。

对于其他任何人,这就是它所做的:

    // code as written above
    Pattern pattern = Pattern.compile(";jsessionid(=\\w+)");
    Matcher m = pattern.matcher(content);

    content = m.replaceAll("");

    Files.write(content, html, Charsets.UTF_8);

这两个链接很有帮助:

于 2012-07-19T11:57:51.427 回答
0

使用 Java正则表达式jsessionid=<ID> up to ?并从内容中替换。

于 2012-07-19T10:48:51.497 回答