Search in sources :

Example 46 with HttpEntity

use of org.apache.http.HttpEntity in project nanohttpd by NanoHttpd.

the class TestHttpServer method testRangeHeaderWithStartPositionOnly.

@Test
public void testRangeHeaderWithStartPositionOnly() throws ClientProtocolException, IOException {
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet("http://localhost:9090/testdir/test.html");
        httpGet.addHeader("range", "bytes=10-");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseString = new String(readContents(entity), "UTF-8");
        Assert.assertThat("The data from the beginning of the file should have been skipped as specified in the 'range' header", responseString, not(containsString("<head>")));
        Assert.assertThat("The response should contain the data from the end of the file since end position was not given in the 'range' header", responseString, containsString("</head>"));
        Assert.assertEquals("The content length should be the length starting from the requested byte", "74", response.getHeaders("Content-Length")[0].getValue());
        Assert.assertEquals("The 'Content-Range' header should contain the correct lengths and offsets based on the range served", "bytes 10-83/84", response.getHeaders("Content-Range")[0].getValue());
        Assert.assertEquals("Response status for a successful range request should be PARTIAL_CONTENT(206)", 206, response.getStatusLine().getStatusCode());
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 47 with HttpEntity

use of org.apache.http.HttpEntity in project nanohttpd by NanoHttpd.

the class GetAndPostIntegrationTest method testPostRequestWithMultipartExtremEncodedParameters.

@Test
public void testPostRequestWithMultipartExtremEncodedParameters() throws Exception {
    this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
    HttpPost httppost = new HttpPost("http://localhost:8192/chin");
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "sfsadfasdf", Charset.forName("UTF-8"));
    reqEntity.addPart("specialString", new StringBody("拖拉图片到浏览器,可以实现预览功能", "text/plain", Charset.forName("UTF-8")));
    reqEntity.addPart("gender", new StringBody("图片名称", Charset.forName("UTF-8")) {

        @Override
        public String getFilename() {
            return "图片名称";
        }
    });
    httppost.setEntity(reqEntity);
    HttpResponse response = this.httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    String responseBody = EntityUtils.toString(entity, "UTF-8");
    assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;gender=图片名称;specialString=拖拉图片到浏览器,可以实现预览功能", responseBody);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 48 with HttpEntity

use of org.apache.http.HttpEntity in project nanohttpd by NanoHttpd.

the class TestNanolets method doGeneralParams.

@Test
public void doGeneralParams() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:9090/general/value1/value2?param3=value3&param4=value4");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body><h1>Url: /general/value1/value2</h1><br><p>Param 'param3' = value3</p><p>Param 'param4' = value4</p>", string);
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 49 with HttpEntity

use of org.apache.http.HttpEntity in project nanohttpd by NanoHttpd.

the class TestNanolets method doSomeBasicMethodTest.

@Test
public void doSomeBasicMethodTest() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost:9090/user/blabla");
    CloseableHttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    String string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: GET<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpPost httppost = new HttpPost("http://localhost:9090/user/blabla");
    response = httpclient.execute(httppost);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: POST<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpPut httpgput = new HttpPut("http://localhost:9090/user/blabla");
    response = httpclient.execute(httpgput);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: PUT<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
    HttpDelete httpdelete = new HttpDelete("http://localhost:9090/user/blabla");
    response = httpclient.execute(httpdelete);
    entity = response.getEntity();
    string = new String(readContents(entity), "UTF-8");
    Assert.assertEquals("<html><body>User handler. Method: DELETE<br><h1>Uri parameters:</h1><div> Param: id&nbsp;Value: blabla</div><h1>Query parameters:</h1></body></html>", string);
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 50 with HttpEntity

use of org.apache.http.HttpEntity in project OpenAttestation by OpenAttestation.

the class ApacheHttpClient method readResponse.

private ApiResponse readResponse(HttpResponse response) throws IOException {
    MediaType contentType = createMediaType(response);
    byte[] content = null;
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream contentStream = entity.getContent();
        if (contentStream != null) {
            content = IOUtils.toByteArray(contentStream);
            contentStream.close();
        }
    }
    return new ApiResponse(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), contentType, content);
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType)

Aggregations

HttpEntity (org.apache.http.HttpEntity)518 HttpResponse (org.apache.http.HttpResponse)185 HttpGet (org.apache.http.client.methods.HttpGet)152 IOException (java.io.IOException)144 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)105 Test (org.junit.Test)93 HttpPost (org.apache.http.client.methods.HttpPost)84 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)83 InputStream (java.io.InputStream)71 ArrayList (java.util.ArrayList)69 Header (org.apache.http.Header)64 StatusLine (org.apache.http.StatusLine)61 URI (java.net.URI)58 NameValuePair (org.apache.http.NameValuePair)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)58 HttpClient (org.apache.http.client.HttpClient)55 StringEntity (org.apache.http.entity.StringEntity)51 InputStreamReader (java.io.InputStreamReader)44 BufferedReader (java.io.BufferedReader)41