Java获取网页信息,如何登录弹出弹出的对话框?

大_爱 2013-11-04 09:59:37
上个图

这是TOMCAT页面,点击左边的status 要求输入用户名和密码,我想用Java代码来实现自动填写用户名和密码登录。不知道这个怎么做,用了httpClient,貌似不行,有其他什么http工具可以实现这个功能么?
...全文
312 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
大_爱 2013-11-04
  • 打赏
  • 举报
回复
引用 9 楼 beefcattlexiaoyang 的回复:
[quote=引用 6 楼 huxiweng 的回复:]

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();
		}
	}

}

亲,我还想问问,你用的jar包是那种jar包。我下载的httpClient和你的不一样啦?[/quote] 感谢!jar包找到了,现在httpclient已经独立出来了。 在这个页面中,http://hc.apache.org/downloads.cgi 找到 Older releases Old releases are available from the archives. 就是找到旧版本 然后 commons-httpclient/ 然后 http://archive.apache.org/dist/httpcomponents/commons-httpclient/binary/ 下载jar包, 这个httpclient 还依赖另外两个jar吧。 这个就应该很好找。
大_爱 2013-11-04
  • 打赏
  • 举报
回复
引用 6 楼 huxiweng 的回复:

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();
		}
	}

}

亲,我还想问问,你用的jar包是那种jar包。我下载的httpClient和你的不一样啦?
大_爱 2013-11-04
  • 打赏
  • 举报
回复
引用 6 楼 huxiweng 的回复:

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();
		}
	}

}

感谢!我试试,我查到一种Authentication认证机制,好像就是这个东西。 解决了的话,我也准备把结果贴在这里。
teemai 2013-11-04
  • 打赏
  • 举报
回复
请求到了后续的xml页面 打印: <?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="xform.xsl" ?><status><jvm><memory free='52663288' total='62980096' max='933953536'/></jvm><connector name='http-8080'><threadInfo maxThreads="200" currentThreadCount="4" currentThreadsBusy="1" /><requestInfo maxTime="96" processingTime="276" requestCount="26" errorCount="9" bytesReceived="0" bytesSent="102224" /><workers><worker stage="R" requestProcessingTime="0" requestBytesSent="0" requestBytesRecieved="0" remoteAddr="?" virtualHost="?" method="?" currentUri="?" currentQueryString="?" protocol="?" /><worker stage="R" requestProcessingTime="0" requestBytesSent="0" requestBytesRecieved="0" remoteAddr="?" virtualHost="?" method="?" currentUri="?" currentQueryString="?" protocol="?" /><worker stage="S" requestProcessingTime="2" requestBytesSent="0" requestBytesReceived="0" remoteAddr="127.0.0.1" virtualHost="127.0.0.1" method="GET" currentUri="/manager/status" currentQueryString="XML=true" protocol="HTTP/1.1" /><worker stage="R" requestProcessingTime="0" requestBytesSent="0" requestBytesRecieved="0" remoteAddr="?" virtualHost="?" method="?" currentUri="?" currentQueryString="?" protocol="?" /></workers></connector><connector name='ajp-8009'><threadInfo maxThreads="200" currentThreadCount="0" currentThreadsBusy="0" /><requestInfo maxTime="0" processingTime="0" requestCount="0" errorCount="0" bytesReceived="0" bytesSent="0" /><workers></workers></connector></status>
teemai 2013-11-04
  • 打赏
  • 举报
回复

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();
		}
	}

}

大_爱 2013-11-04
  • 打赏
  • 举报
回复
引用 4 楼 cl61917380 的回复:
配置文件中需要配置。tomcat-users.xml
嗯,我是在这个文件中配置了,配置了一个用户名和密码,其实不配置也有这个框弹出来
coooliang 2013-11-04
  • 打赏
  • 举报
回复
配置文件中需要配置。tomcat-users.xml
大_爱 2013-11-04
  • 打赏
  • 举报
回复
木有人了。。。。
大_爱 2013-11-04
  • 打赏
  • 举报
回复
引用 1 楼 hjw506848887 的回复:
ajax。。。。。。
用ajax 处理不了。
  • 打赏
  • 举报
回复
ajax。。。。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧