use of org.apache.http.entity.ByteArrayEntity in project okhttp by square.
the class OkApacheClientTest method postByteEntity.
@Test
public void postByteEntity() throws Exception {
server.enqueue(new MockResponse());
final HttpPost post = new HttpPost(server.url("/").url().toURI());
byte[] body = "Hello, world!".getBytes(UTF_8);
post.setEntity(new ByteArrayEntity(body));
client.execute(post);
RecordedRequest request = server.takeRequest();
assertEquals("Hello, world!", request.getBody().readUtf8());
assertEquals(request.getHeader("Content-Length"), "13");
}
use of org.apache.http.entity.ByteArrayEntity in project android_frameworks_base by ParanoidAndroid.
the class AndroidHttpClient method getCompressedEntity.
/**
* Compress data to send to server.
* Creates a Http Entity holding the gzipped data.
* The data will not be compressed if it is too short.
* @param data The bytes to compress
* @return Entity holding the data
*/
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
AbstractHttpEntity entity;
if (data.length < getMinGzipSize(resolver)) {
entity = new ByteArrayEntity(data);
} else {
ByteArrayOutputStream arr = new ByteArrayOutputStream();
OutputStream zipper = new GZIPOutputStream(arr);
zipper.write(data);
zipper.close();
entity = new ByteArrayEntity(arr.toByteArray());
entity.setContentEncoding("gzip");
}
return entity;
}
use of org.apache.http.entity.ByteArrayEntity in project feign by OpenFeign.
the class ApacheHttpClient method toHttpUriRequest.
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.method());
//per request timeouts
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(options.connectTimeoutMillis()).setSocketTimeout(options.readTimeoutMillis()).build();
requestBuilder.setConfig(requestConfig);
URI uri = new URIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
//request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
for (NameValuePair queryParam : queryParams) {
requestBuilder.addParameter(queryParam);
}
//request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// doesn't like us to set it as well.
continue;
}
for (String headerValue : headerEntry.getValue()) {
requestBuilder.addHeader(headerName, headerValue);
}
}
//some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
//request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}
requestBuilder.setEntity(entity);
}
return requestBuilder.build();
}
use of org.apache.http.entity.ByteArrayEntity in project RoboZombie by sahan.
the class RequestParamEndpointTest method testPrimitiveByteArrayEntity.
/**
* <p>Test for a {@link Request} with a {@code byte[]} entity.</p>
*
* @since 1.3.0
*/
@Test
public final void testPrimitiveByteArrayEntity() throws ParseException, IOException {
Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
String subpath = "/primitivebytearrayentity";
byte[] bytes = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 };
ByteArrayEntity bae = new ByteArrayEntity(bytes);
stubFor(put(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200)));
requestEndpoint.primitiveByteArrayEntity(bytes);
verify(putRequestedFor(urlEqualTo(subpath)).withRequestBody(equalTo(EntityUtils.toString(bae))));
}
use of org.apache.http.entity.ByteArrayEntity in project XobotOS by xamarin.
the class AndroidHttpClient method getCompressedEntity.
/**
* Compress data to send to server.
* Creates a Http Entity holding the gzipped data.
* The data will not be compressed if it is too short.
* @param data The bytes to compress
* @return Entity holding the data
*/
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
AbstractHttpEntity entity;
if (data.length < getMinGzipSize(resolver)) {
entity = new ByteArrayEntity(data);
} else {
ByteArrayOutputStream arr = new ByteArrayOutputStream();
OutputStream zipper = new GZIPOutputStream(arr);
zipper.write(data);
zipper.close();
entity = new ByteArrayEntity(arr.toByteArray());
entity.setContentEncoding("gzip");
}
return entity;
}
Aggregations