Example usage for org.apache.commons.httpclient.methods PostMethod PostMethod

List of usage examples for org.apache.commons.httpclient.methods PostMethod PostMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod PostMethod.

Prototype

public PostMethod(String paramString) 

Source Link

Usage

From source file:net.morphbank.mbsvc3.test.SendImageTest.java

public void sendImage(String strURL, String id, String originalFileName, String imageFileName)
        throws Exception {
    File input = new File(imageFileName);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);

    Part[] parts = { new StringPart("id", id), new StringPart("fileName", originalFileName),
            new FilePart("image", originalFileName, input) };
    RequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
    post.setRequestEntity(entity);/* w  w w. j  ava  2 s.co  m*/
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        System.out.println("Trying post");
        int result = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        System.out.println("Response body: ");
        InputStream response = post.getResponseBodyAsStream();
        // int j = response.read(); System.out.write(j);
        for (int i = response.read(); i != -1; i = response.read()) {
            System.out.write(i);
        }
        // System.out.flush();
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
}

From source file:eu.learnpad.rest.utils.internal.DefaultCWRestUtils.java

@Override
public boolean postNotifyModel(String modelId, String type) {
    HttpClient httpClient = getClient();

    String uri = REST_URI + "/learnpad/cw/notify/model";
    PostMethod postMethod = new PostMethod(uri);
    postMethod.addRequestHeader("Accept", "application/xml");
    postMethod.addRequestHeader("Accept-Ranges", "bytes");

    NameValuePair[] queryString = new NameValuePair[2];
    queryString[0] = new NameValuePair("modelid", modelId);
    queryString[1] = new NameValuePair("type", type);
    postMethod.setQueryString(queryString);

    try {//w ww.ja v  a2s .  c  om
        httpClient.executeMethod(postMethod);
        return true;
    } catch (HttpException e) {
        logger.error("Unable to process the notification request to Collaborative Workspace for the '" + modelId
                + "' model (" + type + ").", e);
        return false;
    } catch (IOException e) {
        logger.error("Unable to notify Collaborative Workspace of the '" + modelId + "' model (" + type + ").",
                e);
        return false;
    }
}

From source file:io.hawt.web.JBossPostApp.java

@Test
public void testPostWithCredentials() throws Exception {
    System.out.println("Using URL: " + url + " user: " + userName + " password: " + password);

    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);

    //client.getParams().setAuthenticationPreemptive(true);
    method.setDoAuthentication(true);/*from  w w w.ja v a 2s  . co m*/

    Credentials defaultcreds = new UsernamePasswordCredentials(userName, password);
    client.getState().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
    //client.getState().setProxyCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);

    method.setRequestEntity(new StringRequestEntity(data, "application/json", "UTF-8"));

    int result = client.executeMethod(method);

    System.out.println("Status: " + result);

    String response = method.getResponseBodyAsString();
    System.out.println(response);
}

From source file:com.ikanow.infinit.e.harvest.enrichment.legacy.HttpClientPost.java

private PostMethod createPostMethod() {

    PostMethod method = new PostMethod(CALAIS_URL);

    // Set mandatory parameters
    method.setRequestHeader("x-calais-licenseID", "1232323232");

    // Set input content type
    method.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
    //method.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
    //method.setRequestHeader("Content-Type", "text/raw; charset=UTF-8");

    // Set response/output format
    method.setRequestHeader("Accept", "xml/rdf");
    //method.setRequestHeader("Accept", "application/json");

    // Enable Social Tags processing
    method.setRequestHeader("enableMetadataType", "SocialTags");

    return method;
}

From source file:com.assemblade.client.Login.java

public boolean changePassword(String username, String password, String newPassword) throws CallFailedException {
    PostMethod method = new PostMethod(baseUrl + "/login/changepassword");
    try {/*  ww  w.  j a  v  a2s . co m*/
        method.setQueryString(new NameValuePair[] { new NameValuePair("username", username),
                new NameValuePair("password", password), new NameValuePair("newpassword", newPassword) });
        return executeMethod(method) == 200;
    } finally {
        method.releaseConnection();
    }
}

From source file:htmlwordtag.HtmlWordTag.java

private PostMethod createPostMethod() {

    PostMethod method = new PostMethod(CALAIS_URL);

    // Set mandatory parameters
    method.setRequestHeader("x-calais-licenseID", "guuv7mvdatyk3d5yy6xab8d3");

    // Set input content type
    //method.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
    method.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
    //method.setRequestHeader("Content-Type", "text/raw; charset=UTF-8");

    // Set response/output format
    method.setRequestHeader("Accept", "xml/rdf");
    //method.setRequestHeader("Accept", "application/json");

    // Enable Social Tags processing
    method.setRequestHeader("enableMetadataType", "SocialTags");

    return method;
}

From source file:es.carebear.rightmanagement.client.RestClient.java

public User registerTest(String username, String password) throws IOException {

    HttpClient client = new HttpClient();

    PostMethod method = new PostMethod(getServiceBaseURI() + "/client/users/register/" + username);

    method.addParameter("password", password);

    System.out.println(method.getPath());

    int responseCode = client.executeMethod(method);

    String response = responseToString(method.getResponseBodyAsStream());

    System.out.println(response);

    return XMLHelper.fromXML(response, User.class);
}

From source file:net.oauth.client.OAuthHttpClient.java

/** Send a message to the service provider and get the response. */
@Override/*from w  w  w. j a  va  2 s  . co  m*/
protected OAuthMessage invoke(OAuthMessage message) throws Exception {
    String form = OAuth.formEncode(message.getParameters());
    HttpMethod method;
    if ("GET".equals(message.httpMethod)) {
        method = new GetMethod(message.URL);
        method.setQueryString(form);
        // method.addRequestHeader("Authorization", message
        // .getAuthorizationHeader(serviceProvider.userAuthorizationURL));
        method.setFollowRedirects(false);
    } else {
        PostMethod post = new PostMethod(message.URL);
        post.setRequestEntity(new StringRequestEntity(form, OAuth.FORM_ENCODED, null));
        method = post;
    }
    clientPool.getHttpClient(new URL(method.getURI().toString())).executeMethod(method);
    final OAuthMessage response = new HttpMethodResponse(method);
    int statusCode = method.getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        Map<String, Object> dump = response.getDump();
        OAuthProblemException problem = new OAuthProblemException(
                (String) dump.get(OAuthProblemException.OAUTH_PROBLEM));
        problem.getParameters().putAll(dump);
        throw problem;
    }
    return response;
}

From source file:com.bluexml.xforms.demo.Util.java

/**
 * Authenticates a user with an Alfresco instance.
 * /*from ww w.j  a  v a 2  s. c o  m*/
 * @param host
 *            the address (protocol, hostname, port number) of the host where the BlueXML XForms
 *            webscript is deployed, with NO trailing slash. If NULL, defaults to localhost:8080
 * @param userName
 *            the user name to test, which should be known to Alfresco
 * @param password
 *            the plain text password to test
 * @return true if the authentication succeeded, false if the authentication failed or if an
 *         exception occurred
 */
public static boolean authenticate(String host, String userName, String password) {
    PostMethod post = new PostMethod(host + "service/xforms/auth");

    post.setParameter("username", userName);
    post.setParameter("password", password);

    HttpClient client = new HttpClient();
    try {
        client.executeMethod(post);
    } catch (HttpException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    String result;
    try {
        result = post.getResponseBodyAsString();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    if (result == null) {
        return false;
    }
    result = result.trim();
    return result.equals("success");
}

From source file:com.ifeng.vdn.ip.repository.service.impl.AliIPAddressChecker.java

@Override
public IPAddress ipcheck(String ip) {
    AliIPBean ipaddress = null;//from w  ww .  jav a2s.  c o  m

    String url = "http://ip.taobao.com/service/getIpInfo2.php";

    // Create an instance of HttpClient.
    HttpClient clinet = new HttpClient();

    // Create a method instance.
    PostMethod postMethod = new PostMethod(url);

    // Execute the method.
    // Read the response body.
    try {

        postMethod.setParameter("ip", ip);

        int resultCode = clinet.executeMethod(postMethod);

        if (resultCode == HttpStatus.SC_OK) {
            InputStream responseBody = postMethod.getResponseBodyAsStream();

            ObjectMapper mapper = new ObjectMapper();
            ipaddress = mapper.readValue(responseBody, AliIPBean.class);

            log.info(responseBody.toString());
        } else {
            log.error("Method failedd: [{}] , IP: [{}]", postMethod.getStatusCode(), ip);
        }

    } catch (JsonParseException | JsonMappingException | HttpException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return ipaddress;
}