Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.61 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package se.wip;
  2.  
  3. import org.apache.commons.httpclient.*;
  4. import org.apache.commons.httpclient.methods.*;
  5. import org.apache.commons.httpclient.params.HttpMethodParams;
  6. import java.io.*;
  7.  
  8.  
  9. public class Httpclient{
  10.         public static InputStream in;
  11.         private static String url = "http://rss.cnn.com/rss/cnn_topstories.rss";
  12.        
  13.         public Httpclient() {
  14.                
  15.         }
  16.        
  17.         public void stream() {
  18.                 // Create an instance of HttpClient.
  19.                 HttpClient client = new HttpClient();
  20.  
  21.                 // Create a method instance.
  22.                 GetMethod method = new GetMethod(url);
  23.    
  24.                 // Provide custom retry handler is necessary
  25.                 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  26.                 new DefaultHttpMethodRetryHandler(3, false));
  27.  
  28.                 try {
  29.                         // Execute the method.
  30.                         int statusCode = client.executeMethod(method);
  31.  
  32.                         if (statusCode != HttpStatus.SC_OK) {
  33.                                 System.err.println("Method failed: " + method.getStatusLine());
  34.                         }//end of if
  35.  
  36.                         // Read the response body.
  37.                         //byte[] responseBody = method.getResponseBody();
  38.                         in = method.getResponseBodyAsStream();
  39.                        
  40.      
  41.                         // Deal with the response.
  42.                         // Use caution: ensure correct character encoding and is not binary data
  43.                         //System.out.println(new String(responseBody));
  44.  
  45.      
  46.                 } catch (HttpException e) {
  47.                         System.err.println("Fatal protocol violation: " + e.getMessage());
  48.                         e.printStackTrace();
  49.                 } catch (IOException e) {
  50.                         System.err.println("Fatal transport error: " + e.getMessage());
  51.                         e.printStackTrace();
  52.                 } finally {
  53.         // Release the connection.
  54.         // method.releaseConnection();
  55.                 }  //end of final
  56.         }//end of method
  57.  
  58. }//end of class