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;
}
}
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;
}
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();
}
}
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;
}
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);
}
Aggregations