Receive HTTP Message via apache httpclient

          Posted on 2010-07-28 20:27 祝嘉 閱讀(328) 評論(0)  編輯  收藏 所屬分類: Library 、Code_HttpClient
            1 /*
            2  * @(#) WebVistor.java 
            3  *
            4  * Created on Jul 28, 2010
            5  *
            6  * All rights reserved.
            7  */
            8 package httpclient;
            9 
           10 import java.io.BufferedReader;
           11 import java.io.FileWriter;
           12 import java.io.IOException;
           13 import java.io.InputStreamReader;
           14 import java.net.URI;
           15 import java.net.URISyntaxException;
           16 
           17 import org.apache.http.HttpEntity;
           18 import org.apache.http.HttpResponse;
           19 import org.apache.http.client.ClientProtocolException;
           20 import org.apache.http.client.methods.HttpGet;
           21 import org.apache.http.impl.client.DefaultHttpClient;
           22 import org.apache.log4j.ConsoleAppender;
           23 import org.apache.log4j.Logger;
           24 import org.apache.log4j.PatternLayout;
           25 
           26 /**
           27  * @author 祝嘉 (Mr.Zhujia@gmail.com | TopZhujia@163.com)
           28  * 
           29  */
           30 public class WebVistor {
           31     /**
           32      * Logger for this class
           33      */
           34     private static final Logger logger = Logger.getLogger(WebVistor.class);
           35     static {
           36         logger.addAppender(new ConsoleAppender(new PatternLayout("%m%n")));
           37     }
           38 
           39     public static void main(String[] args) throws URISyntaxException {
           40         if (logger.isDebugEnabled()) {
           41             logger.debug("main(String[]) - start");
           42         }
           43 
           44         WebVistor vistor = new WebVistor();
           45         String uri = "http://www.aygfsteel.com/MrZhujia/"
           46                 + "archive/2010/07/28/327357.html";
           47         vistor.visit(new URI(uri));
           48         vistor.disconnect();
           49 
           50         if (logger.isDebugEnabled()) {
           51             logger.debug("main(String[]) - end");
           52         }
           53     }
           54 
           55     private DefaultHttpClient httpclient = null;
           56     private HttpGet httpget = null;
           57     private HttpResponse response = null;
           58 
           59     public WebVistor() {
           60         httpclient = new DefaultHttpClient();
           61         httpget = new HttpGet();
           62     }
           63 
           64     public void disconnect() {
           65         if (logger.isDebugEnabled()) {
           66             logger.debug("disconnect() - start");
           67         }
           68 
           69         httpclient.getConnectionManager().shutdown();
           70 
           71         if (logger.isDebugEnabled()) {
           72             logger.debug("disconnect() - end");
           73         }
           74     }
           75 
           76     public void visit(URI uri) {
           77         if (logger.isDebugEnabled()) {
           78             logger.debug("visit(URI) - start");
           79         }
           80 
           81         httpget.setURI(uri);
           82         BufferedReader br = null;
           83         FileWriter fout = null;
           84         try {
           85             response = httpclient.execute(httpget);
           86             logger.info("response status: " + response.getStatusLine());
           87             HttpEntity entity = response.getEntity();
           88             if(null == entity){
           89                 throw new Exception("failed, empty entity");
           90             }
           91             br = new BufferedReader(new InputStreamReader(entity.getContent()));
           92             fout = new FileWriter("result.html");
           93 
           94             char[] buf = new char[1024];
           95             int length = -1;
           96             while (-1 != (length = br.read(buf))) {
           97                 fout.write(buf, 0, length);
           98             }
           99 
          100         } catch (ClientProtocolException e) {
          101             logger.error("visit(URI)", e);
          102         } catch (IOException e) {
          103             logger.error("visit(URI)", e);
          104         } catch (Exception e) {
          105             //don't do anything
          106             logger.error("visit(URI)", e);
          107         } finally {
          108             try {
          109                 if (null != fout) {
          110                     fout.flush();
          111                     fout.close();
          112                 }
          113                 if (null != br) {
          114                     br.close();
          115                 }
          116                 response.getEntity().consumeContent();
          117             } catch (IOException e) {
          118                 logger.error("visit(URI)", e);
          119             }
          120         }
          121 
          122         if (logger.isDebugEnabled()) {
          123             logger.debug("visit(URI) - end");
          124         }
          125     }
          126 }
          127 

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 高唐县| 百色市| 孝感市| 大新县| 关岭| 南城县| 凌云县| 应城市| 邻水| 长顺县| 丰顺县| 大悟县| 伊春市| 柳林县| 兴隆县| 兴安盟| 正蓝旗| 崇礼县| 聂拉木县| 巴中市| 萝北县| 景洪市| 二连浩特市| 始兴县| 翁牛特旗| 衡东县| 丰镇市| 桃江县| 白银市| 莱芜市| 孟连| 阜宁县| 柞水县| 玛多县| 苏尼特左旗| 青阳县| 招远市| 淮安市| 清流县| 鄂伦春自治旗| 江门市|