I'm trying to create an HTTP POST request from a simple Java Project.
I need to keep session and cookies through two requests, so I opted for the Apache HttpClient.
The code compiles with no errors and runs, but it returns a zero-length content and I can't understand why.
public class Test {
private static final String CONTENT_TYPE = "Content-Type";
private static final String FORM_URLENCODED = "application/x-www-form-urlencoded";
public static void main(String[] args) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
BasicHttpContext httpCtx = new BasicHttpContext();
CookieStore store = new BasicCookieStore();
httpCtx.setAttribute(HttpClientContext.COOKIE_STORE, store);
String url = "http://myhost:port/app/";
String body = "my body string";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(CONTENT_TYPE, FORM_URLENCODED);
StringEntity entity = new StringEntity(body);
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost, httpCtx);
HttpEntity respentity = response.getEntity();
System.out.println("respentity: " + respentity);
System.out.println("EntityUtils.toString(respentity): " + EntityUtils.toString(respentity));
EntityUtils.consume(respentity);
System.out.println("respentity: " + respentity);
System.out.println("EntityUtils.toString(respentity): " + EntityUtils.toString(respentity));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The result is:
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
Updated: I found out the response status is 302 (Found), when I do the same request from Postman it's 200 (OK).
Can anybody tell me what's wrong with my code, please?
Thanks