www.pudn.com > HKJC2.rar > HttpUtil.java


package hkjc2.logic; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpStatus; 
import org.apache.commons.httpclient.cookie.CookiePolicy; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.params.HttpMethodParams; 
 
public class HttpUtil { 
 
	private boolean useProxy = false; 
	private String host = null; 
	private int port = 80; 
	 
	public boolean isUseProxy() { 
		return useProxy; 
	} 
	public void setUseProxy(boolean useProxy) { 
		this.useProxy = useProxy; 
	} 
	public String getHost() { 
		return host; 
	} 
	public void setHost(String host) { 
		this.host = host; 
	} 
	public int getPort() { 
		return port; 
	} 
	public void setPort(int port) { 
		this.port = port; 
	} 
	 
	public String getHTML(String url) throws Exception { 
		String result = null; 
		HttpClient client = new HttpClient(); 
		if (host != null) { 
			client.getHostConfiguration().setProxy(host, port); 
		} 
	    GetMethod method = new GetMethod(url); 
	    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false)); 
	    method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); 
	    try { 
	    	int statusCode = client.executeMethod(method); 
	    	if (statusCode == HttpStatus.SC_OK) { 
	    		byte[] b = method.getResponseBody(); 
	    		if (b != null) { 
	    			result = new String(b, "Big5"); 
	    		} 
	    	} else { 
	    		throw new Exception("打不开指定的网页"); 
	    	} 
	    } catch (Exception e) { 
	    	throw e; 
	    } finally { 
	    	method.releaseConnection(); 
	    } 
		return result; 
	} 
	public String toChinese(String source) { 
        List toTurn = new ArrayList(); 
        String[] arr = source.split("&#"); 
        for (int i = 0; i < arr.length; i++) { 
            String s = arr[i]; 
            if (s.length() > 0 && s.indexOf(";") > 0) { 
                toTurn.add(s.substring(0, s.indexOf(";"))); 
            } 
        } 
        for (int i = 0; i < toTurn.size(); i++) { 
            String t = toTurn.get(i); 
            char c = (char)Integer.parseInt(t, 10); 
            source = source.replaceAll("&#" + t + ";", "" + c); 
        } 
        return source; 
    } 
}