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

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

Introduction

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

Prototype

public void setRequestContentLength(int paramInt) 

Source Link

Usage

From source file:com.discursive.jccook.httpclient.PostFileExample.java

public static void main(String[] args) throws HttpException, IOException {

    // Configure Logging
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

    HttpClient client = new HttpClient();

    // Create POST method
    String weblintURL = "http://ats.nist.gov/cgi-bin/cgi.tcl/echo.cgi";
    PostMethod method = new PostMethod(weblintURL);

    File file = new File("project.xml");
    method.setRequestBody(new FileInputStream(file));
    method.setRequestContentLength((long) file.length());

    // Execute and print response
    client.executeMethod(method);//from ww w .j a va2  s  .com
    String response = method.getResponseBodyAsString();
    System.out.println(response);

    method.releaseConnection();
}

From source file:com.krawler.esp.utils.HttpPost.java

public String invoke(String soapMessage) {
    try {//from ww  w . jav a  2 s . com
        int statusCode = -1;
        String mUri = "http://localhost:7070/service/soap/";
        // the content-type charset will determine encoding used
        // when we set the request body
        PostMethod method = new PostMethod(mUri);
        // method.setRequestHeader("Content-type",
        // getSoapProtocol().getContentType());
        method.setRequestBody(soapMessage);
        method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_AUTO);

        // if (getSoapProtocol().hasSOAPActionHeader())
        // method.setRequestHeader("SOAPAction", mUri);

        // execute the method.
        HttpClient mClient = new HttpClient();
        statusCode = mClient.executeMethod(method);

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        // Release the connection.
        method.releaseConnection();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary
        // data

        String responseStr = toString(responseBody);
        return responseStr;
    } catch (IOException ex) {
        return ex.toString();
    } catch (Exception ex) {
        return ex.toString();
    }
}

From source file:org.apache.commons.httpclient.demo.PostXMLClient.java

@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {

    File input = new File("test.xml");
    PostMethod post = new PostMethod("http://90.0.12.20:8088/NationWideAdmin/test/PostXMLClient.jsp");

    // /*from w w w .  j a v a2  s  . c  o  m*/
    post.setRequestBody(new FileInputStream(input));

    if (input.length() < Integer.MAX_VALUE) {
        post.setRequestContentLength(input.length());
    } else {
        post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
    }
    // 

    post.setRequestHeader("Content-type", "text/xml; charset=GBK");

    HttpClient httpclient = new HttpClient();
    int result = httpclient.executeMethod(post);
    System.out.println("Response status code: " + result);
    System.out.println("Response body: ");
    System.out.println(post.getResponseBodyAsString());

    post.releaseConnection();
}

From source file:org.openamf.test.RemotingTester.java

public static AMFMessage sendMessage(String gateway, AMFMessage requestMessage)
        throws IOException, HttpException {

    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baOutputStream);
    AMFSerializer serializer = new AMFSerializer(outputStream);
    serializer.serializeMessage(requestMessage);

    PostMethod post = new PostMethod(gateway);
    post.setRequestBody(new ByteArrayInputStream(baOutputStream.toByteArray()));
    post.setRequestContentLength(baOutputStream.size());
    post.setRequestHeader("Content-type", "application/x-amf");

    HttpClient httpclient = new HttpClient();
    int result = httpclient.executeMethod(post);

    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(post.getResponseBody()));

    AMFDeserializer deserializer = new AMFDeserializer(inputStream);
    AMFMessage resposeMessage = deserializer.getAMFMessage();

    // Release current connection to the connection pool once you are done
    post.releaseConnection();/*  ww  w. ja  v a2s.  c  om*/

    return resposeMessage;
}

From source file:org.tizzit.util.spring.httpinvoker.StreamSupportingHttpInvokerRequestExecutor.java

protected PostMethod createPostMethodForStreaming(final HttpInvokerClientConfiguration config)
        throws IOException {
    final PostMethod postMethod = new PostMethod(config.getServiceUrl());
    postMethod.setRequestHeader(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_SERIALIZED_OBJECT_WITH_STREAM);
    postMethod.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
    return postMethod;
}