Search in sources :

Example 26 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project selenium-tests by Wikia.

the class CommonUtils method sendPost.

public static String sendPost(String apiUrl, String[][] param) {
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(apiUrl);
        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
        for (int i = 0; i < param.length; i++) {
            paramPairs.add(new BasicNameValuePair(param[i][0], param[i][1]));
        }
        httpPost.setEntity(new UrlEncodedFormEntity(paramPairs));
        HttpResponse response = null;
        response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    } catch (UnsupportedEncodingException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (ClientProtocolException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    } catch (IOException e) {
        PageObjectLogging.log("sendPost", e, false);
        return null;
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair)

Example 27 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project Activiti by Activiti.

the class BaseSpringRestTestCase method internalExecuteRequest.

protected CloseableHttpResponse internalExecuteRequest(HttpUriRequest request, int expectedStatusCode, boolean addJsonContentType) {
    CloseableHttpResponse response = null;
    try {
        if (addJsonContentType && request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type 
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());
        Assert.assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());
        httpResponses.add(response);
        return response;
    } catch (ClientProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    return null;
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) BasicHeader(org.apache.http.message.BasicHeader) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 28 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException in project DrupalCloud by INsReady.

the class JSONServerClient method systemConnect.

/**
	 * system.connect request for Key Auth
	 */
private void systemConnect() throws ServiceNotAvailableException {
    // Cloud server hand shake
    mPairs.add(new BasicNameValuePair("method", "system.connect"));
    try {
        mSERVER.setEntity(new UrlEncodedFormEntity(mPairs));
        HttpResponse response = mClient.execute(mSERVER);
        InputStream result = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(result));
        JSONObject jso = new JSONObject(br.readLine());
        boolean error = jso.getBoolean("#error");
        String data = jso.getString("#data");
        if (error) {
            throw new ServiceNotAvailableException(this, data);
        }
        jso = new JSONObject(data);
        // Save the sessionid to storage
        SharedPreferences auth = mCtx.getSharedPreferences(mPREFS_AUTH, 0);
        SharedPreferences.Editor editor = auth.edit();
        editor.putString("sessionid", jso.getString("sessid"));
        editor.putLong("sessionid_timestamp", new Date().getTime() / 100);
        editor.commit();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SharedPreferences(android.content.SharedPreferences) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) Date(java.util.Date) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONObject(org.json.JSONObject) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BufferedReader(java.io.BufferedReader)

Example 29 with ClientProtocolException

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

the class HttpClient method processHttpRequest.

/**
     * Perform prepared request and optionally return the response
     * @param httpRequest HttpGet, HttpPost etc
     * @param needResponse do we need the response, if false - then null is returned
     * @return the response from the request as an {@link InputStream} or a {@link String} object
     * @throws FileTransferException
     */
private Object processHttpRequest(HttpUriRequest httpRequest, boolean needResponse, boolean returnAsString) throws FileTransferException {
    HttpEntity responseEntity = null;
    boolean responseNotConsumed = true;
    OutputStream outstream = null;
    String errMsg = "Unable to complete request!";
    String responseAsString = null;
    InputStream responseAsStream = null;
    try {
        // send request
        HttpResponse response = httpClient.execute(httpRequest, httpContext);
        if (200 != response.getStatusLine().getStatusCode()) {
            throw new FileTransferException("Request to '" + httpRequest.getURI() + "' returned " + response.getStatusLine());
        }
        // get the response
        responseEntity = response.getEntity();
        if (!needResponse) {
            responseNotConsumed = true;
        } else {
            if (responseEntity != null) {
                if (!returnAsString) {
                    responseAsStream = responseEntity.getContent();
                } else {
                    int respLengthEstimate = (int) responseEntity.getContentLength();
                    if (respLengthEstimate < 0) {
                        respLengthEstimate = 1024;
                    }
                    outstream = new ByteArrayOutputStream(respLengthEstimate);
                    responseEntity.writeTo(outstream);
                    outstream.flush();
                    responseAsString = outstream.toString();
                }
                responseNotConsumed = false;
            }
        }
    } catch (ClientProtocolException e) {
        log.error(errMsg, e);
        throw new FileTransferException(e);
    } catch (IOException e) {
        log.error(errMsg, e);
        throw new FileTransferException(e);
    } finally {
        if (responseEntity != null && outstream != null) {
            IoUtils.closeStream(outstream);
        }
        if (responseNotConsumed) {
            try {
                // We were not able to properly stream the entity content to the local file system.
                // The next line consumes the entity content closes the underlying stream.
                EntityUtils.consume(responseEntity);
            } catch (IOException e) {
            // we tried our best to release the resources
            }
        }
    }
    log.info("Successfully completed GET request '" + httpRequest.getURI() + "'");
    if (returnAsString) {
        return responseAsString;
    }
    return responseAsStream;
}
Also used : FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 30 with ClientProtocolException

use of org.apache.http.client.ClientProtocolException 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)

Aggregations

ClientProtocolException (org.apache.http.client.ClientProtocolException)83 IOException (java.io.IOException)75 HttpResponse (org.apache.http.HttpResponse)45 HttpPost (org.apache.http.client.methods.HttpPost)33 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)29 HttpClient (org.apache.http.client.HttpClient)28 HttpGet (org.apache.http.client.methods.HttpGet)24 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)20 HttpEntity (org.apache.http.HttpEntity)19 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)19 ArrayList (java.util.ArrayList)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)17 InputStreamReader (java.io.InputStreamReader)14 NameValuePair (org.apache.http.NameValuePair)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 InputStream (java.io.InputStream)12 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 BufferedReader (java.io.BufferedReader)9 StringEntity (org.apache.http.entity.StringEntity)9 JSONException (org.json.JSONException)9