use of org.apache.http.message.BasicHeader in project android-volley by mcxiaoke.
the class HttpHeaderParserTest method parseCaseInsensitive.
@Test
public void parseCaseInsensitive() {
long now = System.currentTimeMillis();
Header[] headersArray = new Header[5];
headersArray[0] = new BasicHeader("eTAG", "Yow!");
headersArray[1] = new BasicHeader("DATE", rfc1123Date(now));
headersArray[2] = new BasicHeader("expires", rfc1123Date(now + ONE_HOUR_MILLIS));
headersArray[3] = new BasicHeader("cache-control", "public, max-age=86400");
headersArray[4] = new BasicHeader("content-type", "text/plain");
Map<String, String> headers = BasicNetwork.convertHeaders(headersArray);
NetworkResponse response = new NetworkResponse(0, null, headers, false);
Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
assertNotNull(entry);
assertEquals("Yow!", entry.etag);
assertEqualsWithin(now + ONE_DAY_MILLIS, entry.ttl, ONE_MINUTE_MILLIS);
assertEquals(entry.softTtl, entry.ttl);
assertEquals("ISO-8859-1", HttpHeaderParser.parseCharset(headers));
}
use of org.apache.http.message.BasicHeader in project android-volley by mcxiaoke.
the class HurlStack method performRequest.
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<String, String>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
response.setEntity(entityFromConnection(connection));
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
if (header.getKey() != null) {
Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
response.addHeader(h);
}
}
return response;
}
use of org.apache.http.message.BasicHeader in project clutchandroid by clutchio.
the class ClutchAPIClient method sendRequest.
private static void sendRequest(String url, boolean post, JSONObject payload, String version, final ClutchAPIResponseHandler responseHandler) {
BasicHeader[] headers = { new BasicHeader("X-App-Version", version), new BasicHeader("X-UDID", ClutchUtils.getUDID()), new BasicHeader("X-API-Version", VERSION), new BasicHeader("X-App-Key", appKey), new BasicHeader("X-Bundle-Version", versionName), new BasicHeader("X-Platform", "Android") };
StringEntity entity = null;
try {
entity = new StringEntity(payload.toString());
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Could not encode the JSON payload and attach it to the request: " + payload.toString());
return;
}
HttpRequestBase request = null;
if (post) {
request = new HttpPost(url);
((HttpEntityEnclosingRequestBase) request).setEntity(entity);
request.setHeaders(headers);
} else {
request = new HttpGet(url);
}
class StatusCodeAndResponse {
public int statusCode;
public String response;
public StatusCodeAndResponse(int statusCode, String response) {
this.statusCode = statusCode;
this.response = response;
}
}
new AsyncTask<HttpRequestBase, Void, StatusCodeAndResponse>() {
@Override
protected StatusCodeAndResponse doInBackground(HttpRequestBase... requests) {
try {
HttpResponse resp = client.execute(requests[0]);
HttpEntity entity = resp.getEntity();
InputStream inputStream = entity.getContent();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readBytes = 0;
byte[] sBuffer = new byte[512];
while ((readBytes = inputStream.read(sBuffer)) != -1) {
content.write(sBuffer, 0, readBytes);
}
inputStream.close();
String response = new String(content.toByteArray());
content.close();
return new StatusCodeAndResponse(resp.getStatusLine().getStatusCode(), response);
} catch (IOException e) {
if (responseHandler instanceof ClutchAPIDownloadResponseHandler) {
((ClutchAPIDownloadResponseHandler) responseHandler).onFailure(e, "");
} else {
responseHandler.onFailure(e, null);
}
}
return null;
}
@Override
protected void onPostExecute(StatusCodeAndResponse resp) {
if (responseHandler instanceof ClutchAPIDownloadResponseHandler) {
if (resp.statusCode == 200) {
((ClutchAPIDownloadResponseHandler) responseHandler).onSuccess(resp.response);
} else {
((ClutchAPIDownloadResponseHandler) responseHandler).onFailure(null, resp.response);
}
} else {
if (resp.statusCode == 200) {
responseHandler.handleSuccessMessage(resp.response);
} else {
responseHandler.handleFailureMessage(null, resp.response);
}
}
}
}.execute(request);
}
use of org.apache.http.message.BasicHeader in project crate by crate.
the class BlobIntegrationTest method testInvalidByteRange.
@Test
public void testInvalidByteRange() throws IOException {
String digest = uploadTinyBlob();
Header[] headers = { new BasicHeader("Range", "bytes=40-58") };
CloseableHttpResponse res = get(blobUri(digest), headers);
assertThat(res.getStatusLine().getStatusCode(), is(416));
assertThat(res.getStatusLine().getReasonPhrase(), is("Requested Range Not Satisfiable"));
assertThat(res.getFirstHeader("Content-Length").getValue(), is("0"));
}
use of org.apache.http.message.BasicHeader in project crate by crate.
the class BlobIntegrationTest method testByteRange.
@Test
public void testByteRange() throws IOException {
String digest = uploadTinyBlob();
Header[] headers = { new BasicHeader("Range", "bytes=8-") };
CloseableHttpResponse res = get(blobUri(digest), headers);
assertThat(res.getFirstHeader("Content-Length").getValue(), is("18"));
assertThat(res.getFirstHeader("Content-Range").getValue(), is("bytes 8-25/26"));
assertThat(res.getFirstHeader("Accept-Ranges").getValue(), is("bytes"));
assertThat(res.getFirstHeader("Expires").getValue(), is("Thu, 31 Dec 2037 23:59:59 GMT"));
assertThat(res.getFirstHeader("Cache-Control").getValue(), is("max-age=315360000"));
assertThat(EntityUtils.toString(res.getEntity()), is("ijklmnopqrstuvwxyz"));
res = get(blobUri(digest), new Header[] { new BasicHeader("Range", "bytes=0-1") });
assertThat(EntityUtils.toString(res.getEntity()), is("ab"));
res = get(blobUri(digest), new Header[] { new BasicHeader("Range", "bytes=25-") });
assertThat(EntityUtils.toString(res.getEntity()), is("z"));
}
Aggregations