Search in sources :

Example 16 with HttpResponse

use of org.apache.http.HttpResponse in project camel by apache.

the class CxfRsRouterTest method testPostConsumerUniqueResponseCode.

@Test
public void testPostConsumerUniqueResponseCode() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customersUniqueResponseCode");
    post.addHeader("Accept", "text/xml");
    StringEntity entity = new StringEntity(POST_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    post.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(post);
        assertEquals(201, response.getStatusLine().getStatusCode());
        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Customer><id>124</id><name>Jack</name></Customer>", EntityUtils.toString(response.getEntity()));
        HttpDelete del = new HttpDelete("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers/124/");
        response = httpclient.execute(del);
        // need to check the response of delete method
        assertEquals(200, response.getStatusLine().getStatusCode());
    } finally {
        httpclient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 17 with HttpResponse

use of org.apache.http.HttpResponse in project camel by apache.

the class CxfRsConsumerTest method testPutConsumer.

@Test
public void testPutConsumer() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + CXT + "/rest/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.addHeader("test", "header1;header2");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 18 with HttpResponse

use of org.apache.http.HttpResponse in project camel by apache.

the class CxfRsConsumerWithBeanTest method sendPutRequest.

private void sendPutRequest(String uri) throws Exception {
    HttpPut put = new HttpPut(uri);
    StringEntity entity = new StringEntity("string");
    entity.setContentType("text/plain");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("c20string", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut)

Example 19 with HttpResponse

use of org.apache.http.HttpResponse in project hadoop by apache.

the class AuthenticatorTestCase method doHttpClientRequest.

private void doHttpClientRequest(HttpClient httpClient, HttpUriRequest request) throws Exception {
    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
        final int httpStatus = response.getStatusLine().getStatusCode();
        Assert.assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
    } finally {
        if (response != null)
            EntityUtils.consumeQuietly(response.getEntity());
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse)

Example 20 with HttpResponse

use of org.apache.http.HttpResponse in project hbase by apache.

the class Client method put.

/**
   * Send a PUT request
   * @param cluster the cluster definition
   * @param path the path or URI
   * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be
   * supplied
   * @param content the content bytes
   * @return a Response object with response detail
   * @throws IOException
   */
public Response put(Cluster cluster, String path, Header[] headers, byte[] content) throws IOException {
    HttpPut method = new HttpPut(path);
    try {
        method.setEntity(new InputStreamEntity(new ByteArrayInputStream(content), content.length));
        HttpResponse resp = execute(cluster, method, headers, path);
        headers = resp.getAllHeaders();
        content = getResponseBody(resp);
        return new Response(resp.getStatusLine().getStatusCode(), headers, content);
    } finally {
        method.releaseConnection();
    }
}
Also used : HttpResponse(org.apache.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

HttpResponse (org.apache.http.HttpResponse)1502 HttpGet (org.apache.http.client.methods.HttpGet)717 Test (org.junit.Test)686 IOException (java.io.IOException)355 TestHttpClient (io.undertow.testutils.TestHttpClient)290 HttpPost (org.apache.http.client.methods.HttpPost)238 HttpClient (org.apache.http.client.HttpClient)237 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)219 HttpEntity (org.apache.http.HttpEntity)192 Header (org.apache.http.Header)178 StringEntity (org.apache.http.entity.StringEntity)138 ArrayList (java.util.ArrayList)104 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)104 InputStream (java.io.InputStream)97 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)81 URI (java.net.URI)77 StatusLine (org.apache.http.StatusLine)76 ClientProtocolException (org.apache.http.client.ClientProtocolException)76 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)71 NameValuePair (org.apache.http.NameValuePair)63