Receive HTTP Message via apache 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.blogjava.net/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 
posted @ 2010-07-28 20:27  MagicLetters  阅读(263)  评论(1编辑  收藏  举报