0

我使用 HttpClient。我使用 JSF 开发了 Web 应用程序。部署到tomcat6.0.29。我正在尝试停止应用程序。但它不起作用。

../conf/tomcat-users.xml 文件内容

<?xml version='1.0' encoding='utf-8'?>
      <tomcat-users>
          <role rolename="manager"/>
          <user username="tomcat" password="s3cret" roles="manager" />
     </tomcat-users>

我的java代码是

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.*;
public class HttpClientTutorial
{
private static String url = "http://localhost:8080/manager/html/stop?path=/jsfproject";
public static void main(String[] args)
{        
    HttpClient client = new HttpClient();

    GetMethod method = new GetMethod(url);

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
   try{

        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK)
        {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        byte[] responseBody = method.getResponseBody();

        System.out.println(new String(responseBody));
    }
    catch (HttpException e)
    {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    }
    catch (IOException e)
    {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    }
    finally
    {            
        method.releaseConnection();
    }
}  
}

但我得到了这样的错误

log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.HttpClient).
log4j:WARN Please initialize the log4j system properly.
Method failed: HTTP/1.1 401 Unauthorized

我也得到了

401 Unauthorized
You are not authorized to view this page. If you have not changed any configuration `files, please examine the file conf/tomcat-users.xml in your installation.

That file will contain the credentials to let you use this webapp.
ou will need to add manager role to the config file listed above. For example:

<role rolename="manager"/>
<user username="tomcat" password="s3cret" roles="manager"/>

For more information - please see the Manager App HOW-TO. 

帮我。提前致谢。

4

1 回答 1

2

为了停止应用程序,Tomcat 需要您在 tomcat-users.xml 中创建的用户的凭据,但您的代码不会尝试提供它们。

这里有一些可能会给你一个线索的clode:

HttpClient client = new HttpClient();
HttpState state = client.getState();

// Set credentials on the client
Credentials credentials = new UsernamePasswordCredentials("tomcat","s3cret");
state.setCredentials( null, null, credentials );

HttpMethod method = new GetMethod(url);
于 2012-12-19T13:28:15.003 回答