有 Java 编程相关的问题?

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

java Liferay注销返回400响应

我试图通过Java点击Liferay注销servlet“c/portal/logout”,但它总是返回400响应:

private void sendPost() throws Exception {

    String url = "localhost:8080/c/portal/logout";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    // add header
    post.setHeader("User-Agent", USER_AGENT);
    HttpResponse response = client.execute(post);
    System.out.println("\nSending 'POST' request to URL : " + url);
    BufferedReader rd = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());

}

共 (1) 个答案

  1. # 1 楼答案

    假设您打算注销用户的会话,最好的方法是在HttpServletResponse引用上调用sendRedirect

    public void myPostAction(ActionRequest request, ActionResponse response) throws Exception {
        // ...
        response.sendRedirect("/c/portal/logout");
    }