HttpClient实现页面登录以及跳转

陌上-花开 发布于 2013/10/24 15:44
阅读 9K+
收藏 1
package test;


import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


import dao.HttpClientDao;


public class HttpClientsanhe {  
static final String LOGON_SITE = "139.16.24.90";  
static final int    LOGON_PORT = 8888;  


public static void main(String[] args) throws Exception{  
HttpClient client = new HttpClient();  
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
HttpClientDao dao = new HttpClientDao();
//模拟登录页面login.action->main.action  
PostMethod post = new PostMethod("/qysds/login.action");  
NameValuePair name = new NameValuePair("name", "wywqex3");      
NameValuePair pass = new NameValuePair("password", "hbeweeedswyx");      
post.setRequestBody(new NameValuePair[]{name,pass});  
int status = client.executeMethod(post);  
System.out.println(post.getResponseBodyAsString());  
post.releaseConnection();  
//查看cookie信息  
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();  
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());  
if (cookies.length == 0) {
dao.insertHtml("http://139.16.24.7:8888/qysds/login.action");
} else {  
for (int i = 0; i < cookies.length; i++) {  
System.out.println(cookies[i].toString());
System.out.println("登录成功");
}  

//访问所需的页面main.action  
GetMethod get = new GetMethod("/qysds/main.action");  
client.executeMethod(get);  
System.out.println(get.getResponseBodyAsString());  
get.releaseConnection();  
}  
}

为什么这个代码没有问题,运行结果得不到自己想要的啊。不能打印出main.action页面信息????

什么原因啊????

加载中
0
陌上-花开
陌上-花开

引用来自“mingshun”的答案

检查一下executeMethod()的返回值,是下面的其中一个就获取响应报头里的Location作为URL发出GET请求:

  • HttpStatus.SC_MOVED_PERMANENTLY 
  • HttpStatus.SC_MOVED_TEMPORARILY 
  • HttpStatus.SC_SEE_OTHER 
  • HttpStatus.SC_TEMPORARY_REDIRECT


executeMethod()这个怎么了?????
0
我不说话
我不说话

兄弟,请求"/qysds/main.action"时,你要把 JSESSIONID 写到Cookie里。要不服务器怎么知道你有登陆权限

用FF浏览器,查看一下登陆后的报文信息……然后再用HttpClient模拟

0
guor
guor

我以前也写过一个类似的,应该是可以用的,jar包版本3.1

package com.test;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 
 * @author lenovo
 */
public class ClientHelper {

	private HttpClient client;

	/**
	 * Default Constructor
	 */
	public ClientHelper() {
		this.client = new HttpClient();
		this.client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
	}

	/**
	 * 登录
	 * @param url 请求URL
	 * @param username 用户名
	 * @param password 密码
	 * @throws HttpException
	 * @throws IOException
	 */
	public void login(String url, String username, String password) throws HttpException, IOException {
		PostMethod method = new PostMethod(url);
		try {
			NameValuePair name = new NameValuePair("username", username);
			NameValuePair pass = new NameValuePair("password", password);
			NameValuePair action = new NameValuePair("action", "login");
			method.setRequestBody(new NameValuePair[] { name, pass, action });
			int status = client.executeMethod(method);
			if (status == HttpStatus.SC_OK) {
				//可在此查看Cookie信息
				//CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
				//Cookie[] cookies = client.getState().getCookies();
				//for (int i = 0; i < cookies.length; i++) {
				//	System.out.println(cookies[i].toString());
				//}
				System.out.println("登录成功!");
			} else {
				throw new HttpException("登录失败,错误代码:" + status);
			}
		} finally {
			method.releaseConnection();
		}
	}

	/**
	 * 抓取一个页面
	 * @param url 页面URL
	 * @throws HttpException
	 * @throws IOException
	 */
	public void getPage(String url) throws HttpException, IOException {
		GetMethod method = new GetMethod(url);
		try {
			int status = client.executeMethod(method);
			if (status == HttpStatus.SC_OK) {
				System.out.println(method.getResponseBodyAsString());
			}
		} finally {
			method.releaseConnection();
		}
	}
}
0
陌上-花开
陌上-花开

引用来自“我不说话”的答案

兄弟,请求"/qysds/main.action"时,你要把 JSESSIONID 写到Cookie里。要不服务器怎么知道你有登陆权限

用FF浏览器,查看一下登陆后的报文信息……然后再用HttpClient模拟

我知道你的意思了,就是如果要是想跳转到main.action这个页面。必须得把头部信息和Cookie传给它。是这样吧?
陌上-花开
陌上-花开
回复 @我不说话 : 请问你一下,你知道如何实现登录不,我这个代码实现不了。不知道是什么问题。
我不说话
我不说话
登陆成功后,响应头里有cookie值 请求其他需要登陆权限的页面时,把cookie值放进到请求头里……
0
陌上-花开
陌上-花开

引用来自“lietome”的答案

我以前也写过一个类似的,应该是可以用的,jar包版本3.1

package com.test;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 
 * @author lenovo
 */
public class ClientHelper {

	private HttpClient client;

	/**
	 * Default Constructor
	 */
	public ClientHelper() {
		this.client = new HttpClient();
		this.client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
	}

	/**
	 * 登录
	 * @param url 请求URL
	 * @param username 用户名
	 * @param password 密码
	 * @throws HttpException
	 * @throws IOException
	 */
	public void login(String url, String username, String password) throws HttpException, IOException {
		PostMethod method = new PostMethod(url);
		try {
			NameValuePair name = new NameValuePair("username", username);
			NameValuePair pass = new NameValuePair("password", password);
			NameValuePair action = new NameValuePair("action", "login");
			method.setRequestBody(new NameValuePair[] { name, pass, action });
			int status = client.executeMethod(method);
			if (status == HttpStatus.SC_OK) {
				//可在此查看Cookie信息
				//CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
				//Cookie[] cookies = client.getState().getCookies();
				//for (int i = 0; i < cookies.length; i++) {
				//	System.out.println(cookies[i].toString());
				//}
				System.out.println("登录成功!");
			} else {
				throw new HttpException("登录失败,错误代码:" + status);
			}
		} finally {
			method.releaseConnection();
		}
	}

	/**
	 * 抓取一个页面
	 * @param url 页面URL
	 * @throws HttpException
	 * @throws IOException
	 */
	public void getPage(String url) throws HttpException, IOException {
		GetMethod method = new GetMethod(url);
		try {
			int status = client.executeMethod(method);
			if (status == HttpStatus.SC_OK) {
				System.out.println(method.getResponseBodyAsString());
			}
		} finally {
			method.releaseConnection();
		}
	}
}
可以加一下你QQ 问你一下吗?
返回顶部
顶部