1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
public class Client implements IClient {
private static Logger log = Logger.getLogger(Client.class.getName());
protected static final String CHARSET = "ISO-8859-1";
private String proxy;
private String port;
protected HttpClient client;
protected GetMethod method;
public Client(){
// Create an instance of HttpClient.
client = new HttpClient();
}
public void setProxy(String proxy, int port)
{
// Configure proxy
client.getHostConfiguration().setProxy(proxy, port);
}
public Reader get(String url)
{
Reader responseBody = null;
log.info("connecting to : "+url);
method = new GetMethod(url);
// method.getParams().setHttpElementCharset(CHARSET);
method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
// Check status
if(statusCode != HttpStatus.SC_OK){
log.error("Method failed : " + method.getStatusLine());
return null;
}
// Read the response body.
responseBody = new BufferedReader(new InputStreamReader( method.getResponseBodyAsStream(),
method.getResponseCharSet()));
FileChannel destinationStream = null;
try {
// File creation
destinationStream = new FileOutputStream("toto.xml").getChannel();
destinationStream.force(true);
// get byte[] from In Stream
byte[] bytes = IOUtils.toByteArray(responseBody);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
// Write stream to file
destinationStream.write(buffer);
log.debug("yoooooooooo");
} catch(FileNotFoundException e) {
log.error(e);
} catch(IOException e) {
log.error(e);
} catch(Exception e) {
log.error(e);
} finally {
if(destinationStream != null){
try{
destinationStream.close();
} catch (IOException e) {}
}
}
} catch (HttpException e) {
log.error("Fatal protocol violation: ", e);
} catch (IOException e) {
log.error("Fatal transport error: ", e);
} catch (Exception e) {
log.error("Exception : ", e);
} finally {
// Release the connection.
if(method!=null){
method.releaseConnection();
log.info("connection released");
}
}
return responseBody;
} |
Partager