/** * */ package org.cipres.restapi; import java.io.BufferedReader; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.log4j.Logger; /** * This the client program sends a HTTP GET message to download the * result file * * * @author lcchan * */ public class ResultClient { private static final Logger LOGGER = Logger.getLogger(ResultClient.class); /** * @param args */ public static void main(String[] args) { // step 1: set URL //String url = "http://lcchan-linux.sdsc.edu:8888/cipres-web/restapi/result/job/6983A511791447CB80FD8AFFCC478D97/file/PAUP_12Tx432C.nex"; String url = "http://treebase.sdsc.edu:8888/cipres-web/restapi/result/job/B904B801287C567089E7FE9FCC6D0A94/file/RAxML_info.Job-536"; // step 2: create the GET method HttpMethod method = new GetMethod(url); method.setFollowRedirects(true); // step 3: initialize HttpClient; HttpClient client = new HttpClient(); BufferedReader reader = null; try { // step 4: execute the GET method int status = client.executeMethod(method); // read the response if (status != -1) { // read response from servlet System.out.println("----------------- Begin Response ------------------------------------"); System.out.println("status code: " + method.getStatusCode()); System.out.println("status line: " + method.getStatusLine()); System.out.println("status text: " + method.getStatusText()); //System.out.println("body\n: " + method.getResponseBodyAsString()); System.out.println("----------------- End Response --------------------------------------"); // if multiple lines have been sent InputStream in = method.getResponseBodyAsStream(); reader = new BufferedReader(new InputStreamReader(in)); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line + "\n"); } System.out.println(buffer.toString()); } // end if } catch (Exception ex) { LOGGER.debug("Exception caught in reading file: " + ex.toString()); } finally { // clean up method.releaseConnection(); try {if (reader != null) reader.close();} catch (Exception ex) {}; } } }