Search in sources :

Example 26 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project openkit-android by OpenKit.

the class AsyncHttpClient method put.

/**
     * Perform a HTTP PUT request and track the Android Context which initiated the request.
     * And set one-time headers for the request
     * @param context the Android Context which initiated the request.
     * @param url the URL to send the request to.
     * @param headers set one-time headers for this request
     * @param entity a raw {@link HttpEntity} to send with the request, for example, use this to send string/json/xml payloads to a server by passing a {@link org.apache.http.entity.StringEntity}.
     * @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle the response.
     */
public void put(Context context, String url, Header[] headers, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpPut(org.apache.http.client.methods.HttpPut)

Example 27 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project platformlayer by platformlayer.

the class ApacheCommonsHttpRequest method setRequestContent.

@Override
public void setRequestContent(final ByteSource data) throws IOException {
    HttpEntityEnclosingRequestBase post = (HttpEntityEnclosingRequestBase) request;
    post.setEntity(new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            return true;
        }

        @Override
        public long getContentLength() {
            try {
                return data.getContentLength();
            } catch (IOException e) {
                throw new IllegalStateException("Error getting content length", e);
            }
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            return data.open();
        }

        @Override
        public void writeTo(OutputStream os) throws IOException {
            InputStream is = data.open();
            try {
                IoUtils.copyToOutputStream(is, os);
            } finally {
                is.close();
            }
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    });
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 28 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project opennms by OpenNMS.

the class NCSNorthbounder method postAlarms.

private void postAlarms(HttpEntity entity) {
    //Need a configuration bean for these
    int connectionTimeout = 3000;
    int socketTimeout = 3000;
    Integer retryCount = 3;
    HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion());
    URI uri = m_config.getURI();
    System.err.println("uri = " + uri);
    final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setSocketTimeout(socketTimeout).setConnectionTimeout(connectionTimeout).setRetries(retryCount).useBrowserCompatibleCookies().dontReuseConnections();
    if ("https".equals(uri.getScheme())) {
        try {
            clientWrapper.useRelaxedSSL("https");
        } catch (final GeneralSecurityException e) {
            throw new NorthbounderException("Failed to configure Relaxed SSL handling.", e);
        }
    }
    final HttpEntityEnclosingRequestBase method = m_config.getMethod().getRequestMethod(uri);
    if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) {
        method.setHeader(HTTP.TARGET_HOST, m_config.getVirtualHost());
    }
    if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) {
        method.setHeader(HTTP.USER_AGENT, m_config.getUserAgent());
    }
    method.setProtocolVersion(httpVersion);
    method.setEntity(entity);
    CloseableHttpResponse response = null;
    try {
        System.err.println("execute: " + method);
        response = clientWrapper.execute(method);
    } catch (ClientProtocolException e) {
        throw new NorthbounderException(e);
    } catch (IOException e) {
        throw new NorthbounderException(e);
    } finally {
        IOUtils.closeQuietly(clientWrapper);
    }
    if (response != null) {
        try {
            int code = response.getStatusLine().getStatusCode();
            final HttpResponseRange range = new HttpResponseRange("200-399");
            if (!range.contains(code)) {
                LOG.warn("response code out of range for uri: {}.  Expected {} but received {}", uri, range, code);
                throw new NorthbounderException("response code out of range for uri:" + uri + ".  Expected " + range + " but received " + code);
            }
        } finally {
            IOUtils.closeQuietly(clientWrapper);
        }
    }
    LOG.debug(response != null ? response.getStatusLine().getReasonPhrase() : "Response was null");
}
Also used : HttpResponseRange(org.opennms.core.utils.HttpResponseRange) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) GeneralSecurityException(java.security.GeneralSecurityException) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) HttpVersion(org.apache.http.HttpVersion) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 29 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project ThinkAndroid by white-cat.

the class AsyncHttpClient method post.

/**
	 * Perform a HTTP POST request and track the Android Context which initiated
	 * the request. Set headers only for this request
	 * 
	 * @param context
	 *            the Android Context which initiated the request.
	 * @param url
	 *            the URL to send the request to.
	 * @param headers
	 *            set headers only for this request
	 * @param entity
	 *            a raw {@link HttpEntity} to send with the request, for
	 *            example, use this to send string/json/xml payloads to a server
	 *            by passing a {@link org.apache.http.entity.StringEntity}.
	 * @param contentType
	 *            the content type of the payload you are sending, for example
	 *            application/json if sending a json payload.
	 * @param responseHandler
	 *            the response handler instance that should handle the response.
	 */
public void post(Context context, String url, Header[] headers, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 30 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project afinal by yangfuhai.

the class FinalHttp method post.

public void post(String url, Header[] headers, HttpEntity entity, String contentType, AjaxCallBack<? extends Object> callBack) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPost(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, callBack);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Aggregations

HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)38 HttpPost (org.apache.http.client.methods.HttpPost)17 IOException (java.io.IOException)9 HttpPut (org.apache.http.client.methods.HttpPut)9 HttpEntity (org.apache.http.HttpEntity)7 HttpResponse (org.apache.http.HttpResponse)6 InputStream (java.io.InputStream)5 Header (org.apache.http.Header)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 NameValuePair (org.apache.http.NameValuePair)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 File (java.io.File)3 HashMap (java.util.HashMap)3 HttpDelete (org.apache.http.client.methods.HttpDelete)3 HttpGet (org.apache.http.client.methods.HttpGet)3 FileEntity (org.apache.http.entity.FileEntity)3 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 LinkedList (java.util.LinkedList)2