HttpClient模仿浏览器cookie请求需要登陆验证的页面

1、淘宝网兑换抵价券页面地址:
http://auction1.taobao.com/auction/coupon/validate_exchange.htm
(该页面需要先登陆淘宝网)

2、具体请求的Action:
http://auction1.taobao.com/auction/coupon/validate_exchange.htm?action=coupon%2Fapply_exchange_action&event_submit_do_winkey_apply=anything

用Get方法也可以,把form参数加到地址栏:

http://auction1.taobao.com/auction/coupon/validate_exchange.htm?action=coupon%2Fapply_exchange_action&event_submit_do_winkey_apply=anything&encash=winkey&winkey=TLD35A0P84

请求上面地址也需要登陆。

3、分析浏览器cookie,打开IE,跟踪页面cookie:



4、具体代码:

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class GetTaobao {

	public static void main(String[] args) {
		HttpClient httpClient = new HttpClient();
		String url = "http://auction1.taobao.com/auction/coupon/validate_exchange.htm?action=coupon%2Fapply_exchange_action&event_submit_do_winkey_apply=anything&encash=winkey&winkey=TLD35A0P84";

		GetMethod getMethod = new GetMethod(url);
		
		getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//下面一行需要注释掉,不然返回数据是gzip压缩后的乱码
		//getMethod.setRequestHeader("Accept-Encoding","gzip, deflate");
		getMethod.setRequestHeader("Accept-Language","zh-cn");
		getMethod.setRequestHeader("Connection","Keep-Alive");
		getMethod.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)");
		getMethod.setRequestHeader("Host","auction1.taobao.com");
		getMethod.setRequestHeader("Cookie","ab=24; ssllogin=; tracknick=honda418; tg=0; _cc_=W5iHLLyFfA%3D%3D; _ad_=KeUKmAWU8kRK0uEGyGeDILt0rx2LXNjnS1iKbArmFo7sRGntneSqx2S39sCoBdou3DWjPzYi; wd=575ec8929dadafeffe02836090e282cd_0db2%2C1447eee8ade9296be6b7da258425dc4e_0db2%2C37b2290e0bd639d66dd4d8af77168ec3_0db1%2C1182a6b21a75703ac5ef344fa3d649e4_0db1%2C3fcce38e4d38df27babbead5c969ce9c_0db2%2Ca0e6427ba253effec0676730d5c77f0a_0db1; ystat_bc_989278=135544014411299247; ystat_bc_1031985=2974207998754953203; ystat_bc_1054537=27069353203149412100; ystat_bc_1056444=14372573111871880121; BX=973hnh14uo4in&b=3&s=v6; cna=6OFMAWOF+lQBAeMdAHmd16M7; ystat_bc_1067067=22398982153378132103; ystat_bc_1087728=8548896732772299499; ystat_bc_1087723=19117999521097358007; ystat_bc_922867=24874028163364390332; batchbuy=; t=437df97fd8c3cd5d80156b001c75330d; cookie2=4aef8e6dedb1d0d1a5f2c81212a99d11; uc1=_yb_=false&_msg_=0&_msg_v=true&cookie21=UtASsssmeWzt&cookie14=UoM%2F0usBU%2BxScw%3D%3D&cookie15=VFC%2FuZ9ayeYq2g%3D%3D&cookie16=URm48syIJ1yk0MX2J7mAAEhTuw%3D%3D&existShop=false&enabledWuiShop=false; _wwmsg_=0%2C0; lastgetwwmsg=MTI0NDU5ODQ2Mg%3D%3D; cookie1=U%2BJ5GUTahHSOO5bIn9bGaA1ayHQNLzyfpduePMzm1rM%3D; _sv_=0; cookie17=UoM%2BH5rp6SQ%3D; _nk_=honda418; _l_g_=Ug%3D%3D");

		try {
			int statusCode = httpClient.executeMethod(getMethod);
			System.out.println(statusCode);
			// 301或者302
			if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
					|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
				Header locationHeader = getMethod.getResponseHeader("location");
				String location = null;
				if (locationHeader != null) {
					location = locationHeader.getValue();
					System.out.println("The page was redirected to:" + location);
				} else {
					System.err.println("Location field value is null.");
				}
				return;
			}
			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();
		}
	}
}

你可能感兴趣的:(apache,windows,浏览器,IE,Exchange)