
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.61 KB | hits: 10 | expires: Never
package se.wip;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.*;
public class Httpclient{
public static InputStream in;
private static String url = "http://rss.cnn.com/rss/cnn_topstories.rss";
public Httpclient() {
}
public void stream() {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}//end of if
// Read the response body.
//byte[] responseBody = method.getResponseBody();
in = method.getResponseBodyAsStream();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
//System.out.println(new String(responseBody));
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
// method.releaseConnection();
} //end of final
}//end of method
}//end of class