Java tutorial
package com.wafersystems.util; /** * Copyright (c) 2006-2008 Wafer System Xi'an Software Corporation. * All Rights Reserved. * * This SOURCE CODE FILE, which has been provided by Wafer System Software as part of * a Wafer System Software product for use ONLY by licensed users of the product, includes * CONFIDENTIAL and PROPRIETARY information of Wafer System Software. * * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS OF THE LICENSE STATEMENT * AND LIMITED WARRANTY FURNISHED WITH THE PRODUCT. * * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD WAFER SYSTEM SOFTWARE, ITS RELATED COMPANIES * AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS OR LIABILITIES ARISING OUT OF * THE USE, REPRODUCTION, OR DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR * LIABILITIES ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF * PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE CODE FILE. */ import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.cookie.CookieSpec; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.log4j.Logger; import com.wafersystems.util.StrUtil; /** * ??Http * * AnDong * 2008-10-18 ?02:03:57 * ? V1.0 */ public class HttpUtil { private static Logger logger = Logger.getLogger(HttpUtil.class); private HttpUtil() { } /** * URL? * * @param hClient HttpClient * @param url URL * * @return * @throws Exception */ public static String openURL(HttpClient hClient, String url) throws Exception { HttpMethod method = null; try { //Get method = new GetMethod(url); // URL int result = hClient.executeMethod(method); //cookie? //getCookie(method.getURI().getHost(), method.getURI().getPort(), "/" , false , hClient.getState().getCookies()); //??? result = checkRedirect(method.getURI().getHost(), method.getURI().getPort(), hClient, method, result); if (result != HttpStatus.SC_OK) logger.error(method.getPath() + "" + method.getStatusLine().toString() + "\r\n" + method.getResponseBodyAsString()); return method.getResponseBodyAsString(); } finally { method.releaseConnection(); } } /** * ?URLCookie? * * */ public static void getCookie(String ip, int port, String path, boolean secure, Cookie[] httpCookies) { CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); Cookie[] cookies = cookiespec.match(ip, port, path, secure, httpCookies); if (cookies.length == 0) logger.debug("Cookie?"); else { logger.debug("Cookie?"); for (int i = 0; i < cookies.length; i++) logger.debug(cookies[i].toString()); } } /** * ?? * * @param ip IP? * @param port ?? * @param hClient HttpClient * @param method HttpMethod * @param lastResult * * @return */ public static int checkRedirect(String ip, int port, HttpClient hClient, HttpMethod method, int lastResult) throws Exception { int result = HttpStatus.SC_OK; if ((lastResult == HttpStatus.SC_MOVED_TEMPORARILY) || (lastResult == HttpStatus.SC_MOVED_PERMANENTLY) || (lastResult == HttpStatus.SC_SEE_OTHER) || (lastResult == HttpStatus.SC_TEMPORARY_REDIRECT)) { //????URL? Header header = method.getResponseHeader("location"); if (header != null) { String newURI = header.getValue(); if (StrUtil.isEmptyStr(newURI)) newURI = "http://" + ip + ":" + port + "/"; //URL??IP?? if (!newURI.startsWith("http")) newURI = "http://" + ip + ":" + port + newURI; logger.warn("??" + newURI); method = new GetMethod(newURI); result = hClient.executeMethod(method); } } return result; } }