POST 使用 CloseableHttpClient 请求
POST Request with CloseableHttpClient
我正在尝试从一个简单的 Java 项目创建一个 HTTP POST 请求。
我需要通过两个请求保持会话和cookies,所以我选择了Apache HttpClient。
代码编译没有错误并运行,但是它 returns 一个零长度的内容,我不明白为什么。
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();
}
}
}
结果是:
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
更新: 我发现响应状态是 302(已找到),当我从 Postman 执行相同的请求时,它是 200(好).
谁能告诉我我的代码有什么问题吗?
谢谢
默认情况下,只会自动跟进导致重定向的 GET
个请求。如果使用 HTTP 301 Moved Permanently
或 302 Found
回答 POST
请求,则不会自动遵循重定向。
这是由 HTTP RFC 2616 指定的:
If the 301 status code is received in response to a request other than
GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.
使用HttpClient 4.2(或更高版本),我们可以将重定向策略设置为LaxRedirectStrategy,此策略放宽了限制关于 HTTP 规范强加的 POST 方法的自动重定向。
因此您可以使用如下方法创建 CloseableHttpClient
实例:
private CloseableHttpClient createHttpClient() {
HttpClientBuilder builder = HttpClientBuilder.create();
return builder.setRedirectStrategy(new LaxRedirectStrategy()).build();
}
并用它来管理 POST 请求。
我正在尝试从一个简单的 Java 项目创建一个 HTTP POST 请求。
我需要通过两个请求保持会话和cookies,所以我选择了Apache HttpClient。
代码编译没有错误并运行,但是它 returns 一个零长度的内容,我不明白为什么。
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();
}
}
}
结果是:
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
respentity: [Content-Length: 0,Chunked: false]
EntityUtils.toString(respentity):
更新: 我发现响应状态是 302(已找到),当我从 Postman 执行相同的请求时,它是 200(好).
谁能告诉我我的代码有什么问题吗?
谢谢
默认情况下,只会自动跟进导致重定向的 GET
个请求。如果使用 HTTP 301 Moved Permanently
或 302 Found
回答 POST
请求,则不会自动遵循重定向。
这是由 HTTP RFC 2616 指定的:
If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
使用HttpClient 4.2(或更高版本),我们可以将重定向策略设置为LaxRedirectStrategy,此策略放宽了限制关于 HTTP 规范强加的 POST 方法的自动重定向。
因此您可以使用如下方法创建 CloseableHttpClient
实例:
private CloseableHttpClient createHttpClient() {
HttpClientBuilder builder = HttpClientBuilder.create();
return builder.setRedirectStrategy(new LaxRedirectStrategy()).build();
}
并用它来管理 POST 请求。