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

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

Introduction

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

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:com.ebt.platform.utility.WebServiceCaller.java

public static String CallCasWebService(String xml) {
    PostMethod postMethod = new PostMethod("http://ws.e-baotong.cn:8085/CASServer.asmx?wsdl");
    String responseString = null;
    try {/*w w w. j  ava 2  s .c  o m*/
        byte[] b = xml.getBytes("utf-8");
        InputStream inS = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity req = new InputStreamRequestEntity(inS, b.length, "text/xml; charset=utf-8");
        postMethod.setRequestEntity(req);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == 200) {
            responseString = new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("WebService??====" + responseString);
        } else {
            System.out.println("WebService??" + statusCode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.ebt.platform.utility.WebServiceCaller.java

private static String CallDatv2WebService(String xml) {
    PostMethod postMethod = new PostMethod("http://datv2.e-baotong.cn:9089/GetVersionData.asmx?wsdl");
    String responseString = null;
    try {/*from ww w  . ja v  a2  s  .c o  m*/
        byte[] b = xml.getBytes("utf-8");
        InputStream inS = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity req = new InputStreamRequestEntity(inS, b.length, "text/xml; charset=utf-8");
        postMethod.setRequestEntity(req);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == 200) {
            responseString = new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("WebService??====" + responseString);
        } else {
            System.out.println("WebService??" + statusCode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.ebt.platform.utility.WebServiceCaller.java

/**
 * ?Json?SMS?//from www .j  a va 2 s  .  com
 * @param json
 * @return
 * @throws IOException
 */
public static String CallSMSWebServiceWithJsonParam(String json) throws IOException {
    PostMethod postMethod = new PostMethod("http://sms.e-baotong.cn:8085/EBaoSMS.asmx");
    String responseString = null;
    String xml = getSMSRequestXml(json);
    try {
        byte[] b = xml.getBytes("utf-8");
        InputStream inS = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity req = new InputStreamRequestEntity(inS, b.length, "text/xml; charset=utf-8");
        postMethod.setRequestEntity(req);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == 200) {
            responseString = new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("WebService??====" + responseString);
        } else {
            System.out.println("WebService??" + statusCode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.ebt.platform.utility.WebServiceCaller.java

/**
 * ?Json?UAC?/*w w w .j  a v  a  2 s. com*/
 * @param json
 * @return
 * @throws IOException
 */
public static String CallUacWebServiceWithJsonParam(String json) throws IOException {
    PostMethod postMethod = new PostMethod("http://UACServer.e-bao.cn:8086/UACService.asmx");
    String responseString = null;
    String xml = getUacRequestXml(json);
    try {
        byte[] b = xml.getBytes("utf-8");
        InputStream inS = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity req = new InputStreamRequestEntity(inS, b.length, "text/xml; charset=utf-8");
        postMethod.setRequestEntity(req);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == 200) {
            responseString = new String(postMethod.getResponseBodyAsString().getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("WebService??====" + responseString);
        } else {
            System.out.println("WebService??" + statusCode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.ebt.platform.utility.WebServiceCaller.java

public static String CallTKWebService(String xml, String webservicePath) {
    PostMethod postMethod = new PostMethod(webservicePath);
    String responseString = null;
    String key = "HOCMojuo1SY=";
    try {//from  w  w  w .  j  a v  a2  s.  c  o  m
        byte[] b = Base64.encode(DESCoder.encrypt(xml.getBytes("GBK"), key)).getBytes("UTF-8");
        InputStream inS = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity req = new InputStreamRequestEntity(inS, b.length, "text/xml; charset=UTF-8");
        postMethod.setRequestEntity(req);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        if (statusCode == 200) {
            //String teString = "Rc8vPD/7VKO5hmhaSEFIs+5Y4+vxCWsgxJRd2T3vtFuyOoJwklhLP+Rom/FPTqnA7XUqxV+tVaOL jr5oD9Kl4YgoVY/VedW7zYzSr95IIs1lB5J8PFws6s2M0q/eSCLNJx2pIfGaAoilvQKp0zzxoQxt 1JFTWwxNzO/kSu5IAj3uhQghuFBSjL8XDayI0z+xf2GfBm0cGCAZsuHXFZZQCn6cqTr7r8rBKKjH Q8nBkewMSwkiOcgqayiox0PJwZHslrOc+35gnEgy9mfmvxgJkgzXjdJsSjlJ4pQvxUHV8OmlA5hY pWLBIFR/4cyH6PY+BkDbVHv07XhjJXJymBAFn4tlHQjkHsKWrYeQWedxU7D1/rUeeOl27kdLMNCB i6OsAhHHQPK5PBqL2H/z3clPHyD6UaNQIHEf/wfQy6xZYmGaqXVgjZ2hIgGWGreKSEIJEzBvIFZm qCMMhE1nM/PAkemyUv9YJr0cvwvQgQ50f26rmc1VjVgnp7+LHpMEPs7AQxl+e3o9WaPaVh24E7A/ HEdLMNCBi6OsPQy6Sec0txkEtMxfrDjcHYxmHlzEujar";
            byte[] responses = Base64.decode(postMethod.getResponseBodyAsString());
            //byte[] responses = Base64.decode(teString);
            responseString = new String(DESCoder.decrypt(responses, key), "UTF-8");
            System.out.println("WebService??====" + responseString);
        } else {
            System.out.println("WebService??" + statusCode);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseString;
}

From source file:com.predic8.membrane.core.interceptor.cbr.XPathCBRInterceptorIntegrationTest.java

private PostMethod createPostMethod() {
    PostMethod post = new PostMethod("http://localhost:3024/");
    post.setRequestEntity(new InputStreamRequestEntity(this.getClass().getResourceAsStream("/cbr/order.xml")));
    post.setRequestHeader(Header.CONTENT_TYPE, MimeType.TEXT_XML_UTF8);
    post.setRequestHeader(Header.SOAP_ACTION, "");
    return post;/*from   ww  w  . j  a v  a2s.c o m*/
}

From source file:net.jadler.JadlerTimeoutIntegrationTest.java

@Test(timeout = 10000L)
public void timeout() throws IOException {
    onRequest().respond().withStatus(201);

    final PostMethod method = new PostMethod("http://localhost:" + port());
    method.setRequestEntity(new StringRequestEntity("postbody", null, null));

    int status = client.executeMethod(method);
    assertThat(status, is(201));/*from w  w w  .jav  a  2  s.c  o  m*/
}

From source file:net.sf.taverna.t2.activities.biomoby.ExecuteAsyncCgiService.java

private static boolean pollAsyncCgiService(String msName, String url, EndpointReference epr, String[] queryIds,
        String[] result) throws MobyException {
    // Needed to remap results
    HashMap<String, Integer> queryMap = new HashMap<String, Integer>();
    for (int qi = 0; qi < queryIds.length; qi++) {
        String queryId = queryIds[qi];
        if (queryId != null)
            queryMap.put(queryId, new Integer(qi));
    }//from  w w w  .j ava  2s. c  o  m

    if (queryMap.size() == 0)
        return false;

    // construct the GetMultipleResourceProperties XML
    StringBuffer xml = new StringBuffer();
    xml.append("<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='" + RESOURCE_PROPERTIES_NS
            + "' xmlns:mobyws='http://biomoby.org/'>");
    for (String q : queryMap.keySet())
        xml.append("<wsrf-rp:ResourceProperty>mobyws:" + STATUS_PREFIX + q + "</wsrf-rp:ResourceProperty>");
    xml.append("</wsrf-rp:GetMultipleResourceProperties>");

    StringBuffer httpheader = new StringBuffer();
    httpheader.append("<moby-wsrf>");
    httpheader.append("<wsa:Action xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">"
            + GET_MULTIPLE_RESOURCE_PROPERTIES_ACTION + "</wsa:Action>");
    httpheader.append(
            "<wsa:To xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" wsu:Id=\"To\">"
                    + url + "</wsa:To>");
    httpheader.append(
            "<mobyws:ServiceInvocationId xmlns:mobyws=\"http://biomoby.org/\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" wsa:IsReferenceParameter=\"true\">"
                    + epr.getServiceInvocationId() + "</mobyws:ServiceInvocationId>");
    httpheader.append("</moby-wsrf>");

    AnalysisEvent[] l_ae = null;
    // First, status from queries
    String response = "";
    // construct the Httpclient
    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "jMoby/Taverna2");
    // create the post method
    PostMethod method = new PostMethod(url + "/status");
    // add the moby-wsrf header (with no newlines)
    method.addRequestHeader("moby-wsrf", httpheader.toString().replaceAll("\r\n", ""));

    // put our data in the request
    RequestEntity entity;
    try {
        entity = new StringRequestEntity(xml.toString(), "text/xml", null);
    } catch (UnsupportedEncodingException e) {
        throw new MobyException("Problem posting data to webservice", e);
    }
    method.setRequestEntity(entity);

    // retry up to 10 times
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(10, true));

    // call the method
    try {
        if (client.executeMethod(method) != HttpStatus.SC_OK)
            throw new MobyException("Async HTTP POST service returned code: " + method.getStatusCode() + "\n"
                    + method.getStatusLine() + "\nduring our polling request");
        response = stream2String(method.getResponseBodyAsStream());
    } catch (IOException e) {
        throw new MobyException("Problem reading response from webservice", e);
    } finally {
        // Release current connection to the connection pool once you
        // are
        // done
        method.releaseConnection();
    }

    if (response != null) {
        l_ae = AnalysisEvent.createFromXML(response);
    }

    if (l_ae == null || l_ae.length == 0) {
        new MobyException("Troubles while checking asynchronous MOBY job status from service " + msName);
    }

    ArrayList<String> finishedQueries = new ArrayList<String>();
    // Second, gather those finished queries
    for (int iae = 0; iae < l_ae.length; iae++) {
        AnalysisEvent ae = l_ae[iae];
        if (ae.isCompleted()) {
            String queryId = ae.getQueryId();
            if (!queryMap.containsKey(queryId)) {
                throw new MobyException(
                        "Invalid result queryId on asynchronous MOBY job status fetched from " + msName);
            }
            finishedQueries.add(queryId);
        }
    }

    // Third, let's fetch the results from the finished queries
    if (finishedQueries.size() > 0) {
        String[] resQueryIds = finishedQueries.toArray(new String[0]);
        for (int x = 0; x < resQueryIds.length; x++) {
            // construct the GetMultipleResourceProperties XML
            xml = new StringBuffer();
            xml.append("<wsrf-rp:GetMultipleResourceProperties xmlns:wsrf-rp='" + RESOURCE_PROPERTIES_NS
                    + "' xmlns:mobyws='http://biomoby.org/'>");
            for (String q : resQueryIds)
                xml.append("<wsrf-rp:ResourceProperty>mobyws:" + RESULT_PREFIX + q
                        + "</wsrf-rp:ResourceProperty>");
            xml.append("</wsrf-rp:GetMultipleResourceProperties>");

            httpheader = new StringBuffer();
            httpheader.append("<moby-wsrf>");
            httpheader.append("<wsa:Action xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">"
                    + GET_MULTIPLE_RESOURCE_PROPERTIES_ACTION + "</wsa:Action>");
            httpheader.append(
                    "<wsa:To xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" wsu:Id=\"To\">"
                            + url + "</wsa:To>");
            httpheader.append(
                    "<mobyws:ServiceInvocationId xmlns:mobyws=\"http://biomoby.org/\" xmlns:wsa=\"http://www.w3.org/2005/08/addressing\" wsa:IsReferenceParameter=\"true\">"
                            + epr.getServiceInvocationId() + "</mobyws:ServiceInvocationId>");
            httpheader.append("</moby-wsrf>");
            client = new HttpClient();
            client.getParams().setParameter("http.useragent", "jMoby/Taverna2");
            // create the post method
            method = new PostMethod(url + "/results");
            // add the moby-wsrf header (with no newlines)
            method.addRequestHeader("moby-wsrf", httpheader.toString().replaceAll("\r\n", ""));

            // put our data in the request
            entity = null;
            try {
                entity = new StringRequestEntity(xml.toString(), "text/xml", null);
            } catch (UnsupportedEncodingException e) {
                throw new MobyException("Problem posting data to webservice", e);
            }
            method.setRequestEntity(entity);

            // retry up to 10 times
            client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(10, true));

            // call the method
            try {
                if (client.executeMethod(method) != HttpStatus.SC_OK)
                    throw new MobyException("Async HTTP POST service returned code: " + method.getStatusCode()
                            + "\n" + method.getStatusLine() + "\nduring our polling request");
                // place the result in the array
                result[x] = stream2String(method.getResponseBodyAsStream());
                // Marking as null
                queryIds[x] = null;
            } catch (IOException e) {
                logger.warn("Problem getting result from webservice\n" + e.getMessage());
            } finally {
                // Release current connection
                method.releaseConnection();
            }
        }

    }
    return finishedQueries.size() != queryMap.size();
}

From source file:be.fedict.eid.pkira.xkmsws.util.HttpUtil.java

public byte[] postMessage(String message) throws XKMSClientException {
    try {//from   w  ww .j  ava  2s  . c  o m
        PostMethod method = new PostMethod(endpointAddress);
        method.setRequestEntity(new StringRequestEntity(message, "text/xml", "utf-8"));
        int resultCode = client.executeMethod(method);
        if (resultCode != 200) {
            throw new XKMSClientException("Error calling XKMS service. Got back status code " + resultCode);
        }

        return method.getResponseBody();
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e); // shouldn't happen
    } catch (HttpException e) {
        throw new XKMSClientException("Error calling XKMS service.", e);
    } catch (IOException e) {
        throw new XKMSClientException("Error calling XKMS service.", e);
    }
}

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

/** Send a message to the service provider and get the response. */
@Override/*from ww w.  ja v a2 s . com*/
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;
}