Search in sources :

Example 91 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project mobile-android by photo.

the class ApiBase method createHttpRequest.

/**
     * Create a HttpUriRequest out of a ApiRequest object.
     * 
     * @param request the ApiRequest for which a HttpUriRequest should be
     *            created
     * @param baseUrl the base server url
     * @param listener Progress Listener with callback on progress
     * @return HttpUriRequest object which will do the request as described in
     *         ApiRequest
     * @throws UnsupportedEncodingException
     */
private HttpUriRequest createHttpRequest(ApiRequest request, String baseUrl, ProgressListener listener) throws UnsupportedEncodingException {
    HttpUriRequest httpRequest = null;
    switch(request.getMethod()) {
        case ApiRequest.GET:
            httpRequest = new HttpGet(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.POST:
            httpRequest = new HttpPost(baseUrl + request.getPath());
            HttpPost httpPost = ((HttpPost) httpRequest);
            if (request.isMime()) {
                // TODO use the multipart when possible (currently server
                // handles it wrong)
                // HttpEntity entity = createMultipartEntity(request);
                // TODO remove this when doing correct multipart
                httpRequest = new HttpPost(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
                httpPost = ((HttpPost) httpRequest);
                HttpEntity entity = createFileOnlyMultipartEntity(request);
                if (listener != null) {
                    httpPost.setEntity(new HttpEntityWithProgress(entity, listener, httpPost));
                } else {
                    httpPost.setEntity(entity);
                }
            } else {
                httpPost.setEntity(new UrlEncodedFormEntity(request.getParameters(), HTTP.UTF_8));
            }
            break;
        case ApiRequest.PUT:
            httpRequest = new HttpPut(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.DELETE:
            httpRequest = new HttpDelete(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
    }
    for (NameValuePair pair : request.getHeaders()) {
        request.addHeader(pair.getName(), pair.getValue());
    }
    return httpRequest;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HttpPut(org.apache.http.client.methods.HttpPut)

Example 92 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class SaveOriginalPostRequestTestCase method executePostRequest.

private HttpResponse executePostRequest(TestHttpClient client, String uri, BasicNameValuePair... parameters) throws IOException {
    HttpPost request = new HttpPost(DefaultServer.getDefaultServerURL() + uri);
    request.setEntity(new UrlEncodedFormEntity(new ArrayList<NameValuePair>(Arrays.asList(parameters))));
    return client.execute(request);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 93 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ServletFormAuthURLRewriteTestCase method testServletFormAuthWithSavedPostBody.

@Test
public void testServletFormAuthWithSavedPostBody() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echo";
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity("String Entity"));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.startsWith("j_security_check"));
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + response);
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("String Entity", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Example 94 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class ParameterEchoTestCase method testPostInStream.

@Test
public void testPostInStream() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/aaa");
        final List<NameValuePair> values = new ArrayList<>();
        values.add(new BasicNameValuePair("param1", "1"));
        values.add(new BasicNameValuePair("param2", "2"));
        values.add(new BasicNameValuePair("param3", "3"));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(values, "UTF-8");
        post.setEntity(data);
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(RESPONSE, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 95 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.

the class DefaultCharsetTestCase method testCharacterEncodingFormParser.

@Test
public void testCharacterEncodingFormParser() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/form");
        post.setEntity(new UrlEncodedFormEntity(Collections.singletonList(new BasicNameValuePair("A©é́ु𝔊", "A©é́ु𝔊")), "UTF-8"));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        byte[] response = HttpClientUtils.readRawResponse(result);
        Assert.assertArrayEquals(UTF8, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13