This is TOMCAT page, click on the left of the status required to enter a user name and password, I want to use Java code to automatically fill in the user name and password. I do not know how to do this , with a httpClient, seemingly not, what other http tool can achieve this function?
------ Solution ---------------------------------------- ----
ajax. . . . . .
------ Solution ---------------------------------------- ----
configuration file needs to be configured . tomcat-users.xml
------ Solution ----------------------------------- ---------
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class AutoLoginTomcat {
/**
* @param args
*/
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(
new AuthScope("127.0.0.1", 8080, null),
new UsernamePasswordCredentials("admin", "admin"));
httpClient.getParams().setAuthenticationPreemptive(true);
GetMethod getMethod = new GetMethod(
"http://127.0.0.1:8080/manager/status?XML=true");
getMethod.setDoAuthentication(true);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
byte[] responseBody = getMethod.getResponseBody();
System.out.println(new String(responseBody));
} catch (HttpException e) {
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
getMethod.releaseConnection();
}
}
}
------ For reference only ----------------------- ----------------
can not deal with ajax .
------ For reference only -------------------------------------- -
wood occupied. . . .
------ For reference only ---------------------------------------
ah , I was in this file is configured , the configuration of a user name and password , in fact, have not configured this box pops up
------ For reference only -------- -------------------------------
subsequent requests to an xml page
printing:
------ For reference only ---------------------------------- -----
thanks ! I try, I found one kind Authentication authentication mechanism , it seems that this thing .
solve the case, I am ready to put the results posted here .
------ For reference only -------------------------------------- -
pro , I would like to ask that you use the jar package is the kind of jar package . I downloaded httpClient and your friends are not the same ?
------ For reference only -------------------------------------- -
pro , I would like to ask that you use the jar package is the kind of jar package . I downloaded httpClient and your friends are not the same ?
thanks ! jar package found , and now has an independent httpclient out.
In this page , http://hc.apache.org/downloads.cgi
found
Older releases
Old releases are available from the archives.
is to find the old version
then commons-httpclient /
then http://archive.apache.org/dist/httpcomponents/commons-httpclient/binary/
download jar package, this also depends on the other two httpclient jar it.
This should be very good looking.
没有评论:
发表评论