⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httputil.java

📁 java写的读取香港马术比赛数据,分析并导入到EXCEL的源码,可供研究用。
💻 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<String> toTurn = new ArrayList<String>();
        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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -