/* ================================================================== * Created [2009-4-27 下午11:32:55] by Jon.King * ================================================================== * TSS * ================================================================== * mailTo:jinpujun@hotmail.com * Copyright (c) Jon.King, 2009-2012 * ================================================================== */ package com.jinhe.tss.core; import java.io.IOException; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 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.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import com.jinhe.tss.core.util.XMLDocUtil; public class HttpClientDemo { // 构造HttpClient的实例 HttpClient httpClient = new HttpClient(); public String fetchWebPageByGet(String url) throws HttpException, IOException { // 创建GET方法的实例 GetMethod getMethod = new GetMethod(url); // 设置成了默认的恢复策略,在发生异常时候将自动重试3次,在这里你也可以设置成自定义的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); getMethod.setRequestHeader("_key", "150644D54411599E315D1876DF8DCA0F"); try { // 执行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // 读取内容 byte[] responseBody = getMethod.getResponseBody(); // 处理内容 return 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(); } return null; } public void loginBBSByPost(String url) throws HttpException, IOException { PostMethod postMethod = new PostMethod(url); // 填入各个表单域的值 NameValuePair[] params = { new NameValuePair("operatorCode", "admin"), new NameValuePair("password", "123456") }; // 将表单的值放入postMethod中 postMethod.setRequestBody(params); // 执行postMethod int statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发 // 301或者302 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 从头中取出转向的地址 Header locationHeader = postMethod.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."); } } // 读取内容 byte[] responseBody = postMethod.getResponseBody(); // 处理内容 System.out.println(XMLDocUtil.dataXml2Doc(new String(responseBody)).selectSingleNode("//key").getText()); Cookie[] cookies = httpClient.getState().getCookies(); for (int i = 0; i < cookies.length; i++) { System.out.println("Cookie(" + cookies[i].getName() + ", " + cookies[i].getValue() + ")"); } postMethod.releaseConnection(); //如果每次访问要共享同一个session,则需要每次设置 httpClient.getState().addCookies(cookies); //其中cookis为上次或者上次以前在同意session期中返回得cookie,即Cookie[] cookies = httpClient.getState().getCookies(); } public void testLoginJCZL() throws HttpException, IOException{ HttpClient httpClient1 = new HttpClient(); PostMethod postMethod = new PostMethod("http://localhost:8088/jczl/login.in"); // 填入各个表单域的值 NameValuePair[] params = { new NameValuePair("operatorCode", "admin"), new NameValuePair("password", "123456") }; // 将表单的值放入postMethod中 postMethod.setRequestBody(params); // 执行postMethod int statusCode = httpClient1.executeMethod(postMethod); String key = null; if(statusCode == HttpStatus.SC_OK){ // 读取内容 byte[] responseBody = postMethod.getResponseBody(); key = XMLDocUtil.dataXml2Doc(new String(responseBody)).selectSingleNode("//key").getText(); Cookie[] cookies = httpClient1.getState().getCookies(); for (int i = 0; i < cookies.length; i++) { System.out.println("Cookie(" + cookies[i].getName() + ", " + cookies[i].getValue() + ")"); } } postMethod.releaseConnection(); //------------------------------------------------------------------------------------------------------------ HttpClient httpClient2 = new HttpClient(); // 创建GET方法的实例 GetMethod getMethod = new GetMethod("http://localhost:8088/jczl/framework.do"); getMethod.setRequestHeader("_key", key); try { // 执行getMethod statusCode = httpClient2.executeMethod(getMethod); if (statusCode == HttpStatus.SC_OK) System.out.println(new String(getMethod.getResponseBody())); } catch (HttpException e) { e.printStackTrace(); // 发生致命的异常,可能是协议不对或者返回的内容有问题 } catch (IOException e) { e.printStackTrace(); // 发生网络异常 } finally { getMethod.releaseConnection(); // 释放连接 } } public static void main(String[] args) throws HttpException, IOException { HttpClientDemo test = new HttpClientDemo(); System.out.println(test.fetchWebPageByGet("http://www.google.com")); // test.loginBBSByPost("http://localhost:8088/jczl/login.in"); // System.out.println(test.fetchWebPageByGet("http://localhost:8088/jczl/framework.do")); test.testLoginJCZL(); } }