下面列出了org.apache.http.client.methods.HttpPost#setProtocolVersion() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Future<HttpResponse> executeRequest(
final ClientProxy client,
final boolean withRequestContent) throws Throwable {
final HttpUriRequest request;
final URI requestUri = URI.create("http://localhost:" + client.listenPort + "/status.html");
if (!withRequestContent) {
HttpGet httpGet = new HttpGet(requestUri);
httpGet.setProtocolVersion(client.requestVersion);
request = httpGet;
} else {
final HttpPost post = new HttpPost(requestUri);
post.setEntity(new StringEntity(REQUEST_CONTENT, StandardCharsets.UTF_8));
post.setProtocolVersion(client.requestVersion);
request = post;
}
log.fine(() -> "executorService:"
+ " .isShutDown()=" + executorService.isShutdown()
+ " .isTerminated()=" + executorService.isTerminated());
return executorService.submit(() -> client.delegate.execute(request));
}
@Override
public String post(String body) throws ConnectTimeoutException, SocketTimeoutException {
/*
* LOG.info(String.format(
* "httpClient获取到的header: %s ; httpClient获取到的body:%s ", headers,
* body));
*/
if (client == null) {
return INIT_FAIL;
}
HttpPost httpPost = new HttpPost(url);
httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
String responseContent;
try {
StringEntity entity = new StringEntity(body == null ? "" : body, "utf-8");
entity.setContentEncoding("utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setConfig(requestConfig);
HttpResponse httpResponse = client.execute(httpPost);
responseContent = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
} catch (ConnectTimeoutException | SocketTimeoutException connectOrReadTimeout) {
throw connectOrReadTimeout;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
httpPost.releaseConnection();
}
return responseContent;
}
protected static HttpPost getPost(String uri) {
HttpPost post = new HttpPost(uri);
post.setProtocolVersion(HttpVersion.HTTP_1_1);
return post;
}