blob: 5d6bc36f334c27de3ed456c1cd863dbe6325ce04 [file] [log] [blame]
/*
* Copyright 2018 OK2 Consulting Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hc.client5.migration.examples;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HttpClient4Example {
public static void main(String... args) throws Exception {
CloseableHttpClient client = HttpClients.custom()
.setSSLSocketFactory(new SSLConnectionSocketFactory(
SSLContexts.createSystemDefault(),
new String[] { "TLSv1.2" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
.setConnectionTimeToLive(1, TimeUnit.MINUTES)
.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(5000)
.build())
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(5000)
.setCookieSpec(CookieSpecs.STANDARD_STRICT)
.build())
.build();
CookieStore cookieStore = new BasicCookieStore();
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
HttpClientContext clientContext = HttpClientContext.create();
clientContext.setCookieStore(cookieStore);
clientContext.setCredentialsProvider(credentialsProvider);
clientContext.setRequestConfig(RequestConfig.custom()
.setConnectTimeout(10000)
.setSocketTimeout(10000)
.setCookieSpec(CookieSpecs.STANDARD)
.build());
JsonFactory jsonFactory = new JsonFactory();
ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
HttpPost httpPost = new HttpPost("https://httpbin.org/post");
List<NameValuePair> requestData = Arrays.asList(
new BasicNameValuePair("name1", "value1"),
new BasicNameValuePair("name2", "value2"));
EntityTemplate requestEntity = new EntityTemplate(outstream -> {
objectMapper.writeValue(outstream, requestData);
outstream.flush();
});
requestEntity.setContentType(ContentType.APPLICATION_JSON.toString());
httpPost.setEntity(requestEntity);
JsonNode responseData = client.execute(httpPost, response -> {
if (response.getStatusLine().getStatusCode() >= 300) {
throw new ClientProtocolException(Objects.toString(response.getStatusLine()));
}
final HttpEntity responseEntity = response.getEntity();
if (responseEntity == null) {
return null;
}
try (InputStream inputStream = responseEntity.getContent()) {
return objectMapper.readTree(inputStream);
}
});
System.out.println(responseData);
client.close();
}
}