Search in sources :

Example 81 with BasicHeader

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));
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) NetworkResponse(com.android.volley.NetworkResponse) BasicHeader(org.apache.http.message.BasicHeader) Cache(com.android.volley.Cache) Test(org.junit.Test)

Example 82 with BasicHeader

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;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 83 with BasicHeader

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BasicHeader(org.apache.http.message.BasicHeader)

Example 84 with BasicHeader

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"));
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 85 with BasicHeader

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"));
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Aggregations

BasicHeader (org.apache.http.message.BasicHeader)128 Header (org.apache.http.Header)67 Test (org.junit.Test)29 List (java.util.List)21 HashMap (java.util.HashMap)19 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicStatusLine (org.apache.http.message.BasicStatusLine)17 ProtocolVersion (org.apache.http.ProtocolVersion)16 StatusLine (org.apache.http.StatusLine)16 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 URL (java.net.URL)15 HttpResponse (org.apache.http.HttpResponse)15 Response (org.elasticsearch.client.Response)10 ArrayList (java.util.ArrayList)9 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 TestHttpResponse (org.robolectric.shadows.httpclient.TestHttpResponse)9 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)8 StringEntity (org.apache.http.entity.StringEntity)8