Search in sources :

Example 26 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class MockHttpClient method execute.

// This is the only one we actually use.
@Override
public HttpResponse execute(HttpUriRequest request, HttpContext context) {
    requestExecuted = request;
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), mStatusCode, "");
    HttpResponse response = new BasicHttpResponse(statusLine);
    response.setEntity(mResponseEntity);
    return response;
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 27 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class BasicNetworkTest method serverError_disableRetries.

@Test
public void serverError_disableRetries() throws Exception {
    for (int i = 500; i <= 599; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
        // expected
        }
        // should not retry any 500 error w/ HTTP 500 retries turned off (the default).
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 28 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class BasicNetworkTest method redirect.

@Test
public void redirect() throws Exception {
    for (int i = 300; i <= 399; i++) {
        MockHttpStack mockHttpStack = new MockHttpStack();
        BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
        mockHttpStack.setResponseToReturn(fakeResponse);
        BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
        Request<String> request = buildRequest();
        request.setRetryPolicy(mMockRetryPolicy);
        doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
        try {
            httpNetwork.performRequest(request);
        } catch (VolleyError e) {
        // expected
        }
        // should not retry 300 responses.
        verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
        reset(mMockRetryPolicy);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 29 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project TaEmCasa by Dionen.

the class BasicNetworkTest method unauthorized.

@Test
public void unauthorized() throws Exception {
    MockHttpStack mockHttpStack = new MockHttpStack();
    BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 401, "Unauthorized");
    mockHttpStack.setResponseToReturn(fakeResponse);
    BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
    Request<String> request = buildRequest();
    request.setRetryPolicy(mMockRetryPolicy);
    doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
    try {
        httpNetwork.performRequest(request);
    } catch (VolleyError e) {
    // expected
    }
    // should retry in case it's an auth failure.
    verify(mMockRetryPolicy).retry(any(AuthFailureError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockHttpStack(com.android.volley.mock.MockHttpStack) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) AuthFailureError(com.android.volley.AuthFailureError) ProtocolVersion(org.apache.http.ProtocolVersion) Test(org.junit.Test)

Example 30 with BasicHttpResponse

use of org.apache.http.message.BasicHttpResponse in project CodeUtils by boredream.

the class LeanCloudHttpUtils method postFile.

public static String postFile(String url, File file) throws Exception {
    // LeanCloud上传限制, 最多1秒1个
    Thread.sleep(1000);
    HashMap<String, String> map = getHeaderMap();
    map.put("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Content-Type", new MimetypesFileTypeMap().getContentType(file));
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    int len;
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(file);
    while ((len = fis.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    out.close();
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // 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);
    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);
        }
    }
    Header contentTypeHeader = response.getHeaders(HTTP.CONTENT_TYPE)[0];
    String responseCharset = parseCharset(contentTypeHeader);
    byte[] bytes = entityToBytes(response.getEntity());
    String responseContent = new String(bytes, responseCharset);
    return responseContent;
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) 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)

Aggregations

BasicHttpResponse (org.apache.http.message.BasicHttpResponse)70 ProtocolVersion (org.apache.http.ProtocolVersion)51 BasicStatusLine (org.apache.http.message.BasicStatusLine)45 StatusLine (org.apache.http.StatusLine)39 HttpResponse (org.apache.http.HttpResponse)33 StringEntity (org.apache.http.entity.StringEntity)25 Header (org.apache.http.Header)24 List (java.util.List)23 IOException (java.io.IOException)19 HttpHost (org.apache.http.HttpHost)19 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 HttpURLConnection (java.net.HttpURLConnection)15 HashMap (java.util.HashMap)15 HttpEntity (org.apache.http.HttpEntity)14 MainResponse (org.elasticsearch.action.main.MainResponse)14 ElasticsearchException (org.elasticsearch.ElasticsearchException)12 RequestLine (org.apache.http.RequestLine)11 BasicRequestLine (org.apache.http.message.BasicRequestLine)11 RestStatus (org.elasticsearch.rest.RestStatus)11