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:codeOrchestra.lcs.license.ActivationReporter.java

public boolean report() {
    try {/* www.j  a  v  a 2s.co m*/
        PostMethod postMethod = new PostMethod(ACTIVATION_URL);
        postMethod.setParameter("sn", serialNumber);
        postMethod.setParameter("fp", getFingerPrint());
        httpClient.executeMethod(postMethod);

        System.out.println("Activation: " + postMethod.getStatusCode());

        return true;
    } catch (Throwable t) {
        // ignore
    }
    return false;
}

From source file:com.touch6.sm.gateway.webchinese.Webchinese.java

public static String batchSend(String url, String uid, String key, String phone, String msg, String contentType,
        String charset) throws CoreException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.addRequestHeader("Content-Type", contentType);//?
    NameValuePair[] data = { new NameValuePair("Uid", uid), new NameValuePair("Key", key),
            new NameValuePair("smsMob", phone), new NameValuePair("smsText", msg) };
    post.setRequestBody(data);//from   ww w.  j a  v  a 2 s . c o  m
    try {
        client.executeMethod(post);
    } catch (Exception e) {
        logger.info("??:", e);
        throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION));
    }
    Header[] headers = post.getResponseHeaders();
    int statusCode = post.getStatusCode();
    System.out.println("statusCode:" + statusCode);
    for (Header h : headers) {
        System.out.println(h.toString());
    }
    String result;
    try {
        result = new String(post.getResponseBodyAsString().getBytes(charset));
        System.out.println(result); //???
    } catch (Exception e) {
        logger.info("??:", e);
        throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION));
    }

    post.releaseConnection();
    return result;
}

From source file:alluxio.util.network.HttpUtils.java

/**
 * Uses the post method to send a url with arguments by http, this method can call RESTful Api.
 *
 * @param url the http url/* ww  w .  jav a2  s  . c  om*/
 * @param timeout milliseconds to wait for the server to respond before giving up
 * @param processInputStream the response body stream processor
 */
public static void post(String url, Integer timeout, IProcessInputStream processInputStream)
        throws IOException {
    Preconditions.checkNotNull(timeout, "timeout");
    Preconditions.checkNotNull(processInputStream, "processInputStream");
    PostMethod postMethod = new PostMethod(url);
    try {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
            InputStream inputStream = postMethod.getResponseBodyAsStream();
            processInputStream.process(inputStream);
        } else {
            throw new IOException("Failed to perform POST request. Status code: " + statusCode);
        }
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:com.google.appsforyourdomain.provisioning.AppsUtil.java

/**
 * Posts the specified postContent to the urlString and
 * returns a JDOM Document object containing the XML response.
 * /*from   w  w w.j  a  v a2  s.c  o m*/
 * @param urlString URL destination
 * @param postContent XML request
 * @return a JDOM Document object containing the XML response
 */
public static Document postHttpRequest(String urlString, String postContent) throws AppsForYourDomainException {
    try {

        // Send content
        final HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(urlString);
        StringRequestEntity sre = new StringRequestEntity(postContent, "text/xml", "UTF-8");
        method.setRequestEntity(sre);
        client.executeMethod(method);

        // Get response
        final SAXBuilder builder = new SAXBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        final Document doc = builder.build(rd);
        return doc;
    } catch (IOException e) {

        // error in URL Connection or reading response
        throw new ConnectionException(e.getMessage());
    } catch (JDOMException e) {

        // error in converting to JDOM Document
        throw new ParseException(e.getMessage());
    }
}

From source file:com.legendshop.central.license.HttpClientLicenseHelper.java

public String postMethod(String paramString) {
    String str = null;//from   w  w  w  .  ja  v  a  2 s  . com
    HttpClient localHttpClient = new HttpClient();
    PostMethod localPostMethod = new PostMethod(this._$1);
    NameValuePair[] arrayOfNameValuePair = new NameValuePair[1];
    arrayOfNameValuePair[0] = new NameValuePair("_ENTITY", paramString);
    localPostMethod.addParameters(arrayOfNameValuePair);
    try {
        localHttpClient.executeMethod(localPostMethod);
        str = localPostMethod.getResponseBodyAsString();
    } catch (Exception localException) {
    } finally {
        localPostMethod.releaseConnection();
    }
    return str;
}

From source file:es.carebear.rightmanagement.client.group.AddUserToGroup.java

public void AddUser(String executing, String target, String group)
        throws BadRequestException, InternalServerErrorException, IOException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/groups/add/" + target + "/" + group);
    postMethod.addParameter("authName", executing);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;//from w  w  w.  j av a 2 s.  c  o  m
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_FORBIDDEN:
        throw new ForbiddenException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:es.carebear.rightmanagement.client.admin.DeleteUser.java

public void deleteUser(String userName, String authName) throws IOException, IllegalArgumentException,
        InternalServerErrorException, BadRequestException, UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/users/delete/" + userName);
    postMethod.addParameter("authName", authName);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;//w  w w .  ja  va2s .  c  o  m
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_UNAUTHORIZED:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:edu.usc.isi.HttpClientPost.java

private PostMethod createPostMethod() {

    PostMethod method = new PostMethod(CALAIS_URL);

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

    // Set input content type
    method.setRequestHeader("Content-Type", "text/raw; 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", "GenericRelations");

    return method;
}

From source file:es.carebear.rightmanagement.client.user.LogInUser.java

public User LogIn(String userName, String password) throws IOException, IllegalArgumentException,
        InternalServerErrorException, BadRequestException, UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/users/login/" + userName);
    postMethod.addParameter("password", password);
    //System.err.println(postMethod.getPath());

    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_CREATED:
        String response = XMLHelper.fromStreamToXML(postMethod.getResponseBodyAsStream());
        return XMLHelper.fromXML(response, User.class);
    case HttpStatus.SC_FORBIDDEN:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    default:/*from w  w  w.j a  v  a  2 s  .  c  o  m*/
        throw new UnknownResponseException((new Integer(responseCode)).toString());

    }

}

From source file:es.carebear.rightmanagement.client.admin.CreateUser.java

public User createUser(String authName, String userName, String password)
        throws IOException, IllegalArgumentException, InternalServerErrorException, BadRequestException,
        UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/users/create/" + userName);
    postMethod.addParameter("creatorName", authName);
    postMethod.addParameter("password", password);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_CREATED:
        String response = postMethod.getResponseBodyAsString();
        return XMLHelper.fromXML(response, User.class);
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_UNAUTHORIZED:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    default://w  ww .  java2  s.co  m
        throw new UnknownResponseException((new Integer(responseCode)).toString());

    }

}