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で作成したユーザーの資格情報を必要としますが、コードはそれらを提供しようとはしません。

ここにあなたに手がかりを与えるかもしれないいくつかのクロードがあります:

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 に答える