import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;/**
 * @author 林瑞涵
 * @date 2011-6-30下午08:31:21
 * @name HTTPBase.java
 */
public class HTTPBase {
    private CookieManager manager = new CookieManager(); // 必要的东西    /**
     * Post发送消息
     *
     * @param connection
     * @param message
     * @throws IOException
     */
    public URLConnection sendMessage(URLConnection connection, String message)
            throws IOException {
        connection.setDoOutput(true);        PrintStream out = new PrintStream(connection.getOutputStream());
        out.println(message);
        out.flush();
        return connection;
    }    /**
     * 显示Response消息
     *
     * @param connection
     * @param CharsetName
     * @return
     * @throws URISyntaxException
     * @throws IOException
     */
    public String getMessage(URLConnection connection, String CharsetName)
            throws URISyntaxException, IOException {
        // 添加Cookie
        Map<String, List<String>> head = connection.getHeaderFields();        Set headSet = head.entrySet();
        Iterator it = headSet.iterator();
        while (it.hasNext()) {
            String str = it.next().toString();
            System.out.println(str);
        }        InputStream in = connection.getInputStream();
        Scanner scanner = new Scanner(in, CharsetName);
        StringBuffer message = new StringBuffer();
        while (scanner.hasNextLine()) {
            String str = new String(scanner.nextLine());
            message.append(str);
        }
        return message.toString();
    }    /**
     * 添加Cookie
     *
     * @param connection
     * @throws URISyntaxException
     */
    public void addCookie(URLConnection connection) throws URISyntaxException {
        Map<String, List<String>> head = connection.getHeaderFields();        // 将规则改掉,接受所有的 Cookie
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);        // 保存这个定制的 CookieManager
        CookieHandler.setDefault(manager);        List<String> list = head.get("Set-Cookie");
        if (list != null && !list.isEmpty()) {
            for (String cookies : list) {
                String[] cookie1 = cookies.split(";");
                String[] cookie2 = cookie1[0].split("=");
           
                HttpCookie cook = new HttpCookie(cookie2[0], cookie2[1]);
                manager.getCookieStore().add(connection.getURL().toURI(), cook);
            }
        }
    }    /**
     * 得到Cookie
     * @param conn
     * @return
     * @throws URISyntaxException
     */
    public String getCookie(URLConnection conn) throws URISyntaxException {
        CookieStore store = manager.getCookieStore();
        List<HttpCookie> list = store.get(conn.getURL().toURI());
        StringBuffer cookies = new StringBuffer();
        for (HttpCookie cookie : list) {
            cookies.append(cookie.getName() + "=" + cookie.getValue() + ";");
        }
        System.out.println(cookies);
        return cookies.toString();
    }
}