Example usage for org.apache.commons.httpclient HttpClient executeMethod

List of usage examples for org.apache.commons.httpclient HttpClient executeMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient executeMethod.

Prototype

public int executeMethod(HttpMethod paramHttpMethod) throws IOException, HttpException 

Source Link

Usage

From source file:com.infosupport.service.LPRServiceCaller.java

public static void postData(String urlString, File file) {
    try {//from  w w w  .  j a  va2s .  c  o m
        PostMethod postMessage = new PostMethod(urlString);
        Part[] parts = { new FilePart("lpimage", file), new StringPart("country", "eu"),
                new StringPart("nonce", "123456789") };
        postMessage.setRequestEntity(new MultipartRequestEntity(parts, postMessage.getParams()));
        HttpClient client = new HttpClient();
        client.executeMethod(postMessage);
    } catch (IOException e) {
        Log.w(TAG, "IOException, waarschijnlijk geen internet connectie aanwezig...");
    }
}

From source file:de.mpg.imeji.presentation.metadata.extractors.BasicExtractor.java

public static List<String> extractTechMd(Item item) throws Exception {
    List<String> techMd = new ArrayList<String>();
    URI uri = item.getFullImageUrl();
    String imageUrl = uri.toURL().toString();
    GetMethod method = new GetMethod(imageUrl);
    method.setFollowRedirects(false);//from w  w w. j  a  v a 2 s.co m
    String userHandle = null;
    userHandle = LoginHelper.login(PropertyReader.getProperty("imeji.escidoc.user"),
            PropertyReader.getProperty("imeji.escidoc.password"));
    method.addRequestHeader("Cookie", "escidocCookie=" + userHandle);
    HttpClient client = new HttpClient();
    client.executeMethod(method);
    InputStream input = method.getResponseBodyAsStream();
    ImageInputStream iis = ImageIO.createImageInputStream(input);
    Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
    if (readers.hasNext()) {
        // pick the first available ImageReader
        ImageReader reader = readers.next();
        // attach source to the reader
        reader.setInput(iis, true);
        // read metadata of first image
        IIOMetadata metadata = reader.getImageMetadata(0);
        String[] names = metadata.getMetadataFormatNames();
        int length = names.length;
        for (int i = 0; i < length; i++) {
            displayMetadata(techMd, metadata.getAsTree(names[i]));
        }
    }
    return techMd;
}

From source file:att.jaxrs.client.Category.java

public static Category[] getCategories() {
    GetMethod get = new GetMethod(Constants.SELECT_ALL_CATEGORY_OPERATION);
    CategoryCollection collection = new CategoryCollection();

    HttpClient httpClient = new HttpClient();
    try {/*from   www  .  ja  v  a  2  s .c  om*/
        int result = httpClient.executeMethod(get);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        collection = Marshal.unmarshal(CategoryCollection.class, get.getResponseBodyAsStream());

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        get.releaseConnection();

    }
    if (null != collection.getCategories() && collection.getCategories().length > 0) {
        return collection.getCategories();
    } else {
        System.out.println("unmarshalling returned empty collection");
    }
    return null;
}

From source file:cz.muni.fi.pa165.creatures.rest.client.services.utils.CRUDServiceHelper.java

/**
 * Helper methods which executes an HTTP {@code method} and writes the
 * output to the standard output (e.g. console). Return codes of the HTTP
 * responses are present so we can verify what happened at the server side.
 *
 * @param method method to send to the server side
 *//*from  w w  w  .j  a v  a  2 s  .c o  m*/
public static void send(HttpMethodBase method) {
    HttpClient httpClient = new HttpClient();
    try {

        int result = httpClient.executeMethod(method);
        System.out.println(RESPONSE_STATUS_CODE + result);
        ResponseCode response = ResponseCode.fromInt(result);
        if (response != ResponseCode.NOT_FOUND && response != ResponseCode.SERVER_ERROR
                && response != ResponseCode.FORBIDDEN) {
            System.out.println(RESPONSE_HEADER);

            Header[] headers = method.getResponseHeaders();
            for (Header h : headers) {
                System.out.println(h.toString());
            }

            InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream());
            BufferedReader br = new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
    } catch (ConnectException ex) {
        logger.log(Level.WARNING, CONNECTION_EXCEPTION_MSG);
    } catch (IOException ex) {
        logger.log(Level.INFO, ex.getMessage());
    } finally {
        method.releaseConnection();
    }
}

From source file:demo.jaxrs.client.Client.java

private static void handleHttpMethod(HttpMethod httpMethod) throws Exception {
    HttpClient client = new HttpClient();

    try {/*w  ww  .j a  v  a  2  s .c o m*/
        int statusCode = client.executeMethod(httpMethod);
        System.out.println("Response status : " + statusCode);

        Response.Status status = Response.Status.fromStatusCode(statusCode);

        if (status == Response.Status.OK) {
            System.out.println(httpMethod.getResponseBodyAsString());
        } else if (status == Response.Status.FORBIDDEN) {
            System.out.println("Authorization failure");
        } else if (status == Response.Status.UNAUTHORIZED) {
            System.out.println("Authentication failure");
        }
        System.out.println();

    } finally {
        // release any connection resources used by the method
        httpMethod.releaseConnection();
    }
}

From source file:ca.uvic.cs.tagsea.wizards.TagNetworkSender.java

public static synchronized void send(String xml, IProgressMonitor sendMonitor, int id)
        throws InvocationTargetException {
    sendMonitor.beginTask("Uploading Tags", 100);
    sendMonitor.subTask("Saving Temporary file...");
    File location = TagSEAPlugin.getDefault().getStateLocation().toFile();
    File temp = new File(location, "tagsea.temp.file.txt");
    if (temp.exists()) {
        String message = "Unable to send tags. Unable to create temporary file.";
        IOException ex = new IOException(message);
        throw (new InvocationTargetException(ex, message));
    }//from  www.  j ava 2 s .c o  m
    try {
        FileWriter writer = new FileWriter(temp);
        writer.write(xml);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationTargetException(e);
    }
    sendMonitor.worked(5);
    sendMonitor.subTask("Uploading Tags...");
    String uploadScript = "http://stgild.cs.uvic.ca/cgi-bin/tagSEAUpload.cgi";
    PostMethod post = new PostMethod(uploadScript);

    String fileName = getToday() + ".txt";
    try {
        Part[] parts = { new StringPart("KIND", "tag"), new FilePart("MYLAR" + id, fileName, temp) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);
        String resp = getData(post.getResponseBodyAsStream());
        if (status != 200) {
            IOException ex = new IOException(resp);
            throw (ex);
        }
    } catch (IOException e) {
        throw new InvocationTargetException(e, e.getLocalizedMessage());
    } finally {
        sendMonitor.worked(90);
        sendMonitor.subTask("Deleting Temporary File");
        temp.delete();
        sendMonitor.done();
    }

}

From source file:au.org.ala.commonui.util.WebUtils.java

/**
 * Retrieve contentMap as InputStream./*  w ww  .  j  a va  2s  . co  m*/
 * 
 * @param url
 * @return
 * @throws Exception
 */
public static InputStream getUrlContent(String url) throws Exception {
    HttpClient httpClient = new HttpClient();
    GetMethod gm = new GetMethod(url);
    httpClient.executeMethod(gm);
    return gm.getResponseBodyAsStream();
}

From source file:com.glaf.core.util.http.CommonsHttpClientUtils.java

/**
 * ??GET/*from   w  ww  .ja va2s .  c  om*/
 * 
 * @param url
 *            ??
 * @param encoding
 *            
 * @param dataMap
 *            ?
 * 
 * @return
 */
public static String doGet(String url, String encoding, Map<String, String> dataMap) {
    GetMethod method = null;
    String content = null;
    try {
        method = new GetMethod(url);
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
        if (dataMap != null && !dataMap.isEmpty()) {
            NameValuePair[] nameValues = new NameValuePair[dataMap.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                String name = entry.getKey().toString();
                String value = entry.getValue();
                nameValues[i] = new NameValuePair(name, value);
                i++;
            }
            method.setQueryString(nameValues);
        }
        HttpClient client = new HttpClient();
        int status = client.executeMethod(method);
        if (status == HttpStatus.SC_OK) {
            content = method.getResponseBodyAsString();
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        if (method != null) {
            method.releaseConnection();
            method = null;
        }
    }
    return content;
}

From source file:com.glaf.core.util.http.CommonsHttpClientUtils.java

/**
 * ??POST/*  ww w .  j  a va  2  s  .c  o  m*/
 * 
 * @param url
 *            ??
 * @param encoding
 *            
 * @param dataMap
 *            ?
 * 
 * @return
 */
public static String doPost(String url, String encoding, Map<String, String> dataMap) {
    PostMethod method = null;
    String content = null;
    try {
        method = new PostMethod(url);
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);
        if (dataMap != null && !dataMap.isEmpty()) {
            NameValuePair[] nameValues = new NameValuePair[dataMap.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                String name = entry.getKey().toString();
                String value = entry.getValue();
                nameValues[i] = new NameValuePair(name, value);
                i++;
            }
            method.setRequestBody(nameValues);
        }
        HttpClient client = new HttpClient();
        int status = client.executeMethod(method);
        if (status == HttpStatus.SC_OK) {
            content = method.getResponseBodyAsString();
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        if (method != null) {
            method.releaseConnection();
            method = null;
        }
    }
    return content;
}

From source file:com.mycompany.neo4jprotein.neoStart.java

static public int getServerStatus() {
    int status = 500;
    try {/*from www .  jav  a2 s  . co m*/

        HttpClient client = new HttpClient();
        GetMethod mGet = new GetMethod(SERVER_ROOT_URI);
        status = client.executeMethod(mGet);
        mGet.releaseConnection();
    } catch (Exception e) {
        System.out.println("Exception in connecting to neo4j : " + e);
    }

    return status;
}