Search in sources :

Example 36 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project sling by apache.

the class DistributionUtils method assertPostResource.

private static String assertPostResource(SlingInstance slingInstance, int status, String path, byte[] bytes) throws IOException {
    Request request = slingInstance.getRequestBuilder().buildPostRequest(path);
    if (bytes != null) {
        ByteArrayEntity entity = new ByteArrayEntity(bytes);
        request.withEntity(entity);
    }
    return slingInstance.getRequestExecutor().execute(request.withCredentials(slingInstance.getServerUsername(), slingInstance.getServerPassword())).assertStatus(status).getContent();
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Request(org.apache.sling.testing.tools.http.Request)

Example 37 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project dubbo by alibaba.

the class HttpClientConnection method sendRequest.

public void sendRequest() throws IOException {
    request.setEntity(new ByteArrayEntity(output.toByteArray()));
    this.response = httpClient.execute(request);
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 38 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project 360-Engine-for-Android by 360.

the class HttpConnectionThread method postHTTPRequest.

/**
 * Posts the serialized data to the RPG and synchronously grabs the
 * response.
 *
 * @param postData The post data to send to the RPG.
 * @param uri The URL to send the request to.
 * @param contentType The content type to send as, usually
 *            "application/binary)
 * @return The response data as a byte array.
 * @throws An exception if the request went wrong after HTTP_MAX_RETRY_COUNT
 *             retries.
 */
public HttpResponse postHTTPRequest(byte[] postData, URI uri, String contentType) throws Exception {
    HttpResponse response = null;
    if (null == postData) {
        return response;
    }
    mRetryCount++;
    if (uri != null) {
        logI("RpgHttpConnectionThread.postHTTPRequest()", "HTTP Requesting URI " + uri.toString() + " " + contentType);
        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader("Content-Type", contentType);
        httpPost.addHeader("User-Agent", "PeopleAndroidClient/1.0");
        httpPost.addHeader("Cache-Control", "no-cache");
        httpPost.setEntity(new ByteArrayEntity(postData));
        try {
            response = mHttpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
            // repeat the request N times
            if (mRetryCount < Settings.HTTP_MAX_RETRY_COUNT) {
                return postHTTPRequest(postData, uri, contentType);
            } else {
                throw new Exception("Could not post request " + e);
            }
        }
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 39 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project 360-Engine-for-Android by 360.

the class PollThread method postHTTPRequest.

/**
 * Posts an HTTP request with data to a URL under a certain content type.
 * The method will retry HTTP_MAX_RETRY_COUNT number of times until it
 * throws an exception.
 *
 * @param postData A byte array representing the data to be posted.
 * @param uri The URI to post the data to.
 * @param contentType The content type to post under.
 * @return The response of the server.
 * @throws Exception Thrown if the request failed for HTTP_MAX_RETRY_COUNT
 *             number of times.
 */
private HttpResponse postHTTPRequest(byte[] postData, URI uri, String contentType) throws Exception {
    HttpResponse response = null;
    if (null == postData) {
        return response;
    }
    mRetryCount++;
    if (uri != null) {
        Log.d("POLLTIMETEST", "POLL Requesting URI " + uri.toString());
        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader("Content-Type", contentType);
        httpPost.setEntity(new ByteArrayEntity(postData));
        Log.d("POLLTIMETEST", "POLL Requesting URI " + httpPost.getRequestLine());
        try {
            response = mHttpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
            if (mRetryCount < Settings.HTTP_MAX_RETRY_COUNT) {
                postHTTPRequest(postData, uri, contentType);
            } else {
                throw e;
            }
        }
    }
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ClientProtocolException(org.apache.http.client.ClientProtocolException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 40 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project gocd by gocd.

the class GoHttpClientHttpInvokerRequestExecutor method doExecuteRequest.

@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
    HttpPost postMethod = new HttpPost(config.getServiceUrl());
    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    entity.setContentType(getContentType());
    postMethod.setEntity(entity);
    BasicHttpContext context = null;
    if (environment.useSslContext()) {
        context = new BasicHttpContext();
        context.setAttribute(HttpClientContext.USER_TOKEN, goAgentServerHttpClient.principal());
    }
    try (CloseableHttpResponse response = goAgentServerHttpClient.execute(postMethod, context)) {
        validateResponse(response);
        InputStream responseBody = getResponseBody(response);
        return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)78 HttpEntity (org.apache.http.HttpEntity)28 HttpPost (org.apache.http.client.methods.HttpPost)24 HttpResponse (org.apache.http.HttpResponse)20 IOException (java.io.IOException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)9 InputStream (java.io.InputStream)7 JSONArray (org.json.JSONArray)7 URISyntaxException (java.net.URISyntaxException)5 Header (org.apache.http.Header)5 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 ContentType (org.apache.http.entity.ContentType)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5