package us.codecraft.spider.downloader;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.log4j.Logger;
import us.codecraft.spider.Page;
import us.codecraft.spider.Request;
import us.codecraft.spider.Site;
import us.codecraft.spider.selector.Html;
import us.codecraft.spider.selector.PlainText;
import us.codecraft.spider.utils.UrlUtils;


/**
 * User: cairne
 * Date: 13-4-21
 * Time: 下午12:15
 */
public class HttpClientDownloader implements Downloader {

    private Logger logger = Logger.getLogger(getClass());

    @Override
    public Page download(Request request, Site site) {
        logger.info("downloading page " + request.getUrl());
        HttpClient httpClient = HttpClientPool.getInstance().getClient(site);
        try {
            HttpGet httpGet = new HttpGet(request.getUrl());
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (site.getAcceptStatCode().contains(statusCode)) {
                String content = IOUtils.toString(httpResponse.getEntity().getContent(),
                        site.getEncoding() == null ? httpResponse.getEntity().getContentType().getValue() : site.getEncoding());
                Page page = new Page();
                page.setHtml(new Html(UrlUtils.fixAllRelativeHrefs(content, request.getUrl())));
                page.setUrl(new PlainText(request.getUrl()));
                page.setRequest(request);
                return page;
            } else {
                logger.warn("code error " + statusCode);
            }
        } catch (Exception e) {
            logger.warn("download page " + request.getUrl() + " error", e);
        }
        return null;
    }
}

@@ -1130,24 +1130,274 @@
 tusCode)) %7B%0A
+                if (site.getEncoding() == null)%7B%0A                    String value = httpResponse.getEntity().getContentType().getValue();%0A                    site.setEncoding(new PlainText(value).r(%22charset=(%5B%5E%5C%5Cs%5D+)%22).toString());%0A                %7D%0A
             
@@ -1501,91 +1501,8 @@
     
-site.getEncoding() == null ? httpResponse.getEntity().getContentType().getValue() :
  sit

fix charset bug