use of org.apache.http.entity.StringEntity in project crate by crate.
the class BlobHttpIntegrationTest method put.
protected CloseableHttpResponse put(String uri, String body) throws IOException {
HttpPut httpPut = new HttpPut(Blobs.url(address, uri));
if (body != null) {
StringEntity bodyEntity = new StringEntity(body);
httpPut.setEntity(bodyEntity);
}
return executeAndDefaultAssertions(httpPut);
}
use of org.apache.http.entity.StringEntity in project hive by apache.
the class JIRAService method publishComments.
void publishComments(String comments) {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
URL apiURL = new URL(mUrl);
httpClient.getCredentialsProvider().setCredentials(new AuthScope(apiURL.getHost(), apiURL.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(mUser, mPassword));
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute("preemptive-auth", new BasicScheme());
httpClient.addRequestInterceptor(new PreemptiveAuth(), 0);
HttpPost request = new HttpPost(url);
ObjectMapper mapper = new ObjectMapper();
StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments)));
request.addHeader("Content-Type", "application/json");
request.setEntity(params);
HttpResponse httpResponse = httpClient.execute(request, localcontext);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() != 201) {
throw new RuntimeException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
mLogger.info("JIRA Response Metadata: " + httpResponse);
} catch (Exception e) {
mLogger.error("Encountered error attempting to post comment to " + mName, e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
use of org.apache.http.entity.StringEntity in project hive by apache.
the class PTestClient method post.
private <S extends GenericResponse> S post(Object payload, boolean agressiveRetry) throws Exception {
EndPointResponsePair endPointResponse = Preconditions.checkNotNull(REQUEST_TO_ENDPOINT.get(payload.getClass()), payload.getClass().getName());
HttpPost request = new HttpPost(mApiEndPoint + endPointResponse.getEndpoint());
try {
String payloadString = mMapper.writeValueAsString(payload);
StringEntity params = new StringEntity(payloadString);
request.addHeader("content-type", "application/json");
request.setEntity(params);
if (agressiveRetry) {
mHttpClient.setHttpRequestRetryHandler(new PTestHttpRequestRetryHandler());
}
HttpResponse httpResponse = mHttpClient.execute(request);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() != 200) {
throw new IllegalStateException(statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());
}
String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
@SuppressWarnings("unchecked") S result = (S) endPointResponse.getResponseClass().cast(mMapper.readValue(response, endPointResponse.getResponseClass()));
Status.assertOK(result.getStatus());
if (System.getProperty("DEBUG_PTEST_CLIENT") != null) {
System.err.println("payload " + payloadString);
if (result instanceof TestLogResponse) {
System.err.println("response " + ((TestLogResponse) result).getOffset() + " " + ((TestLogResponse) result).getStatus());
} else {
System.err.println("response " + response);
}
}
Thread.sleep(1000);
return result;
} finally {
request.abort();
}
}
use of org.apache.http.entity.StringEntity in project Android by hmkcode.
the class MainActivity method POST.
public static String POST(String url, Person person) {
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", person.getName());
jsonObject.accumulate("country", person.getCountry());
jsonObject.accumulate("twitter", person.getTwitter());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
use of org.apache.http.entity.StringEntity in project pinot by linkedin.
the class DruidThroughput method main.
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
final int numQueries = QUERIES.length;
final Random random = new Random(RANDOM_SEED);
final AtomicInteger counter = new AtomicInteger(0);
final AtomicLong totalResponseTime = new AtomicLong(0L);
final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS);
for (int i = 0; i < NUM_CLIENTS; i++) {
executorService.submit(new Runnable() {
@Override
public void run() {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:8082/druid/v2/?pretty");
post.addHeader("content-type", "application/json");
CloseableHttpResponse res;
while (true) {
try (BufferedReader reader = new BufferedReader(new FileReader(QUERY_FILE_DIR + File.separator + random.nextInt(numQueries) + ".json"))) {
int length = reader.read(BUFFER);
post.setEntity(new StringEntity(new String(BUFFER, 0, length)));
}
long start = System.currentTimeMillis();
res = client.execute(post);
res.close();
counter.getAndIncrement();
totalResponseTime.getAndAdd(System.currentTimeMillis() - start);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
long startTime = System.currentTimeMillis();
while (true) {
Thread.sleep(REPORT_INTERVAL_MILLIS);
double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
int count = counter.get();
double avgResponseTime = ((double) totalResponseTime.get()) / count;
System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: " + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
}
}
Aggregations