A dynamic SSL configuration has been created on IBM WebSphere that embeds a certificate in outgoing requests, you do not need to specify the paths to jks in the code. How can I configure sending a rest request so that HttpClient does not search for certificates in IBM jdk if the request comes via https?
CloseableHttpClient httpClient = null;
HttpClientBuilder builder = HttpClientBuilder
.create()
.useSystemProperties();
httpClient = builder.build();
HttpPost request = new HttpPost("https://test.org:8081/add");
// add request headers
request.addHeader("custom-key", "rev");
request.addHeader(HttpHeaders.USER_AGENT, "lebot");
CloseableHttpResponse response = httpClient.execute(request);
Related
How do I bypass certificate verification errors with Apache HttpComponents HttpClient 5.1?
I've found a working solution to bypass such errors in HttpClient 4.5 that suggests customizing HttpClient instance:
HttpClient httpClient = HttpClients
.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
But it is not applicable to HttpClient 5.1, as setSSLContext and setSSLHostnameVerifier methods do not exist in HttpClientBuilder (which HttpClients.custom() returns).
There are several specialized builders in HC 5.1 that can be used to do the same:
CloseableHttpClient httpclient = HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContextBuilder.create()
.loadTrustMaterial(TrustAllStrategy.INSTANCE)
.build())
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build())
.build())
.build();
I am working on the integration testcases, as part of that different testcase i have set http timeout for a request means i would like to issue the http request to server but timeout should happen from client side before it receives the request. Since i am using CXF, i have changed as according the solution provided in official site,
How to configure the HTTPConduit for the SOAP Client?
I already a question exists,
changing client timeout for a particular request in Apache CXF
My problem,
I have java classes generated from cxf codegen plugin, where a interface is generated which has all the soap web service operations.
URL wsdl = getClass().getResource("wsdl/CustomerService_1.wsdl");
QName serviceName = new QName("srv.retail.app:ws:customer:1", "CustomerService_1");
QName portName =
new QName("srv.retail.app:ws:customer:1", "CustomerService_1PortTypeSoap11");
CustomerService_1 service = new CustomerService_1(wsdl, serviceName);
CustomerService1PortType customerServicePortType = service.getPort(portName, CustomerService1PortType.class);
Client client = ClientProxy.getClient(customerServicePortType);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(36000);
httpClientPolicy.setAllowChunking(false);
httpClientPolicy.setReceiveTimeout(300);
http.setClient(httpClientPolicy);
Tried different ways those are not working, will be great if you give inputs on this.
I have a running code that i am already using to connect to a server using SSL. The API i am using is commons-httpclient-3.1.jar and i cant not upgrade the jar.
The servers have upgraded to only use TSL 1.2.
Now my existing code is not working.
My current code is
HttpClient httpClient = new HttpClient();
PostMethod post = new PostMethod("https://XYZserver");
post.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
File f = new File("C://sampleXml.xml");
RequestEntity entity = new FileRequestEntity(f, "Application/xml");
post.setRequestEntity(entity);
int statusCode = httpClient.executeMethod(post);
If i modify my code as per link How to force Commons HTTPClient 3.1 to use TLS 1.2 only for HTTPS? then my code is working fine.
I have few questions (As i was unable to find their answers?)
Why do we require a separate class CustomHttpsSocketFactory. Is it possible to modify existing code to use TLS
Is there any other way to do?
I have developed a Java Applet that works properly in a standard environment (no proxies). On the contrary if the applet is running on a client located behind a proxy, it correctly downloads files and accedes resources with simple http GET methods (java.net) but http POST method using Apache httpclient doesn't work.
I'm using:
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.useSystemProperties();
CloseableHttpClient httpclient = clientBuilder.build();
to retrieve credentials for proxy authentication (these credentials have been already used by JVM, because I can accede external resources using java.net).
The execution of the POST method results in a 407 error (Proxy authentication required). Why useSystemProperties() can't retrieve the credentials for authentication? How can I retrieve username and passoword for the proxy?
I am using Apache HttpClient library to connect to url. The network in which i am doing has a secure proxy to it. when i am using the java.net package to connect to the url i just have to add the
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
no proxy userid and password is needed to be passed but when i am trying to connect through httpclient i am getting 407 proxy authentication error.My code is:
HttpHost proxy = new HttpHost("xyz.abc.com",8080,"http");
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
Proxy is using NTML authentication.I don't want to pass userid and password.
I have upgraded to httpclient 4.2 and this version has out of box NTML support. Just need to add following lines to the code
HttpClient httpclient = new DefaultHttpClient();
NTCredentials creds = new NTCredentials("user", "pwd", "myworkstation", "microsoft.com");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
For further reading Httpclent authentication scheme u can refer http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e947
But my question is still open, why HttpClent is not picking the system proxy as simple java program does.
In order for the system properties to be picked up, you could use SystemDefaultHttpClient instead of DefaultHttpClient.
As of HttpClient 4.3, this class has been deprecated in favor of HttpClientBuilder:
HttpClient hc = new HttpClientBuilder().useSystemProperties().build();