Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

In this page you can find the example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity.

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:com.sender.request.SenderUtil.java

public static void post() throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/ServicioWebUtil/ServicioUtil?wsdl");
    String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:util=\"http://util.servicio.com/\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <util:saludar/>\n"
            + "   </soapenv:Body>\n" + "</soapenv:Envelope>";
    HttpEntity entity = new ByteArrayEntity(xml.getBytes("UTF-8"));
    post.setEntity(entity);// ww w.  jav a  2s. c o  m
    HttpResponse response = client.execute(post);
    String result = EntityUtils.toString(response.getEntity());

    System.out.println("Resultado: " + result);
}

From source file:email.mandrill.SendMail.java

public static MessageResponses sendMail(Message message) {
    try {//from w w w . j  av  a 2 s.c  om
        logger.log(Level.INFO, "Message:" + message.toJSONString());

        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/messages/send.json");

        String request = message.toJSONString();
        HttpEntity entity = new ByteArrayEntity(request.getBytes("UTF-8"));
        httppost.setEntity(entity);
        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();

        if (responseEntity != null) {
            InputStream instream = responseEntity.getContent();
            try {
                StringWriter writer = new StringWriter();
                IOUtils.copy(instream, writer, "UTF-8");
                String theString = writer.toString();

                MessageResponses messageResponses = new MessageResponses(theString);
                //Do whateveer is needed with the response.
                logger.log(Level.INFO, theString);
                return messageResponses;
            } finally {
                instream.close();
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, util.Utility.logMessage(e, "Exception while updating org name:", null));
    }
    return null;
}

From source file:apidemo.APIDemo.java

public static String sendPost(String url, byte[] data)
        throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException {
    //        logger.info("url: " + url);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new ByteArrayEntity(data));

    try (CloseableHttpResponse res = httpclient.execute(httpPost)) {
        HttpEntity entity = res.getEntity();

        InputStream inputStream = entity.getContent();
        String sResponse = IOUtils.toString(inputStream, "UTF-8");

        return sResponse;
    }//from  w  w  w.j a va  2 s  .c o m
}

From source file:com.sinfonier.util.JSonUtils.java

public static void sendPostDucksBoard(Map<String, Object> json, String url, String apiKey) {
    HttpClient httpClient = new DefaultHttpClient();
    Gson gson = new GsonBuilder().create();
    String payload = gson.toJson(json);

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(payload.getBytes(Charset.forName("UTF-8"))));
    post.setHeader("Content-type", "application/json");

    try {//w ww  .j  a  v a 2  s  .  c  o  m
        post.setHeader(new BasicScheme().authenticate(new UsernamePasswordCredentials(apiKey, "x"), post));
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }

    try {
        HttpResponse response = httpClient.execute(post);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.thoughtcrime.securesms.mms.MmsSendHelper.java

private static byte[] makePost(MmsConnectionParameters parameters, byte[] mms)
        throws ClientProtocolException, IOException {
    Log.w("MmsSender", "Sending MMS1 of length: " + mms.length);
    try {/*from  ww  w.j  av  a 2  s . com*/
        HttpClient client = constructHttpClient(parameters);
        URI hostUrl = new URI(parameters.getMmsc());
        HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpPost request = new HttpPost(parameters.getMmsc());
        ByteArrayEntity entity = new ByteArrayEntity(mms);

        entity.setContentType("application/vnd.wap.mms-message");

        request.setEntity(entity);
        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownlader", use);
        throw new IOException("Bad URI syntax");
    }
}

From source file:io.github.kitarek.elasthttpd.builder.DummyHttpRequestConsumer.java

public void consumeRequest(HttpRequest request, HttpResponse response) {
    response.setEntity(new ByteArrayEntity(getAsciiBytes("SELFTEST OK")));
}

From source file:com.nominanuda.hyperapi.ByteArrayEntityEncoder.java

@Override
protected HttpEntity encodeInternal(AnnotatedType p, byte[] barr) {
    String ct = Check.ifNullOrBlank(p.mediaType(), defaultContentType);
    ByteArrayEntity entity = new ByteArrayEntity(barr);
    entity.setContentType(ct);/*from   w ww  .  j  a v a2  s . c  om*/
    return entity;
}

From source file:io.tourniquet.junit.http.rules.examples.HttpClientHelper.java

public static HttpPost post(String url, byte[] data) throws UnsupportedEncodingException {

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(data));
    return post;//  w w  w. ja  v a2s .co  m
}

From source file:com.nominanuda.hyperapi.JsonAnyValueEntityEncoder.java

@Override
protected HttpEntity encodeInternal(AnnotatedType p, Object value) {
    byte[] payload = STRUCT.toJsonString(value).getBytes(Strings.UTF8);
    ByteArrayEntity e = new ByteArrayEntity(payload);
    e.setContentType(HttpProtocol.CT_APPLICATION_JSON_CS_UTF8);
    return e;/*from www.ja va 2s . c o m*/
}

From source file:com.nominanuda.hyperapi.DataStructJsonEntityEncoder.java

@Override
protected HttpEntity encodeInternal(AnnotatedType p, DataStruct value) {
    byte[] payload = STRUCT.toJsonString(value).getBytes(Strings.UTF8);
    ByteArrayEntity e = new ByteArrayEntity(payload);
    e.setContentType(HttpProtocol.CT_APPLICATION_JSON_CS_UTF8);
    return e;/*from www .ja  va2 s .c o m*/
}