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;
}
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);
}
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();
}
}
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();
}
}
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();
}
}
Aggregations