Search in sources :

Example 11 with HttpEntityEnclosingRequestBase

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

the class FinalHttp method post.

public <T> void post(String url, Header[] headers, AjaxParams params, String contentType, AjaxCallBack<T> callBack) {
    HttpEntityEnclosingRequestBase request = new HttpPost(url);
    if (params != null)
        request.setEntity(paramsToEntity(params));
    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)

Example 12 with HttpEntityEnclosingRequestBase

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

the class FinalHttp method put.

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

Example 13 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project tdi-studio-se by Talend.

the class DynamicsCRMClient method createAndExecuteRequest.

/**
     * Created and executes a request
     * 
     * @param uri the request URI
     * @param httpEntity the entity to send.
     * @param method HTTP method
     * 
     * @return the response to the request.
     * @throws ServiceUnavailableException
     */
protected HttpResponse createAndExecuteRequest(URI uri, HttpEntity httpEntity, HttpMethod method) throws ServiceUnavailableException {
    boolean hasRetried = false;
    while (true) {
        try {
            httpClient = (DefaultHttpClient) httpClientFactory.create(null, null);
            HttpRequestBase request = null;
            if (method == HttpMethod.POST) {
                request = new HttpPost(uri);
            } else if (method == HttpMethod.PATCH) {
                request = new HttpPatch(uri);
            } else if (method == HttpMethod.DELETE) {
                request = new HttpDelete(uri);
            } else {
                throw new HttpClientException("Unsupported operation:" + method);
            }
            request.addHeader(HttpHeader.AUTHORIZATION, "Bearer " + authResult.getAccessToken());
            if (request instanceof HttpEntityEnclosingRequestBase) {
                ((HttpEntityEnclosingRequestBase) request).setEntity(httpEntity);
            }
            HttpResponse response = httpClient.execute(request);
            if (isResponseSuccess(response.getStatusLine().getStatusCode())) {
                request.releaseConnection();
                EntityUtils.consume(response.getEntity());
                return response;
            } else {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && !hasRetried) {
                    refreshToken();
                    hasRetried = true;
                    continue;
                }
                HttpEntity entity = response.getEntity();
                String message = null;
                if (entity != null) {
                    message = odataClient.getDeserializer(ContentType.JSON).toError(entity.getContent()).getMessage();
                } else {
                    message = response.getStatusLine().getReasonPhrase();
                }
                throw new HttpClientException(message);
            }
        } catch (Exception e) {
            throw new HttpClientException(e);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpClientException(org.apache.olingo.client.api.http.HttpClientException) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) HttpPatch(org.apache.olingo.client.core.http.HttpPatch) URISyntaxException(java.net.URISyntaxException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) ODataClientErrorException(org.apache.olingo.client.api.communication.ODataClientErrorException) HttpClientException(org.apache.olingo.client.api.http.HttpClientException)

Example 14 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project ats-framework by Axway.

the class HttpClient method performUploadFile.

@Override
protected void performUploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
    checkClientInitialized();
    final String uploadUrl = constructUploadUrl(remoteDir, remoteFile);
    log.info("Uploading " + uploadUrl);
    HttpEntityEnclosingRequestBase uploadMethod;
    Object uploadMethodObject = customProperties.get(HTTP_HTTPS_UPLOAD_METHOD);
    if (uploadMethodObject == null || !uploadMethodObject.toString().equals(HTTP_HTTPS_UPLOAD_METHOD__POST)) {
        uploadMethod = new HttpPut(uploadUrl);
    } else {
        uploadMethod = new HttpPost(uploadUrl);
    }
    String contentType = DEFAULT_HTTP_HTTPS_UPLOAD_CONTENT_TYPE;
    Object contentTypeObject = customProperties.get(HTTP_HTTPS_UPLOAD_CONTENT_TYPE);
    if (contentTypeObject != null) {
        contentType = contentTypeObject.toString();
    }
    FileEntity fileUploadEntity = new FileEntity(new File(localFile), ContentType.parse(contentType));
    uploadMethod.setEntity(fileUploadEntity);
    HttpResponse response = null;
    try {
        // add headers specified by the user
        addRequestHeaders(uploadMethod);
        // upload the file
        response = httpClient.execute(uploadMethod, httpContext);
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode < 200 || responseCode > 206) {
            // 204 No Content - there was a file with same name on same location, we replaced it
            throw new FileTransferException("Uploading '" + uploadUrl + "' returned '" + response.getStatusLine() + "'");
        }
    } catch (ClientProtocolException e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } catch (IOException e) {
        log.error("Unable to upload file!", e);
        throw new FileTransferException(e);
    } finally {
        // the UPLOAD returns response body on error
        if (response != null && response.getEntity() != null) {
            HttpEntity responseEntity = response.getEntity();
            // the underlying stream has been closed
            try {
                EntityUtils.consume(responseEntity);
            } catch (IOException e) {
            // we tried our best to release the resources
            }
        }
    }
    log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + this.hostname);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) FileEntity(org.apache.http.entity.FileEntity) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) ClientProtocolException(org.apache.http.client.ClientProtocolException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) File(java.io.File)

Example 15 with HttpEntityEnclosingRequestBase

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

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)

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