java - REST webservice call for HTTPS link - 400 Bad Request error
问题描述
Using SOAP UI, I am able to successfully invoke the REST GET call with basic authentication
GET https://app.test.com/testing/rest/authentication-point/authentication HTTP/1.1
Accept-Encoding: gzip,deflate
Authorization: Basic aPOjYVclOmIzABFhZjVpJES=
Host: app.test.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
I received response code as 200.
Similar request, When I tried to invoke via java client, it is giving 400 status code.
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("User", "Password"));
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = null;
response = client.execute(
new HttpGet("https://app.test.com/testing/rest/authentication-point/authentication"),
context);
int statusCode = response.getStatusLine().getStatusCode();
This code worked properly when the host was HTTP. Recently a VIP is added and made as HTTPS, after which it is not working. Please suggest a fix for this.
解决方案
You need to use ssl with http client:
TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(
acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 8443, sf));
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("User", "Password"));
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
DefaultHttpClient httpClient = new DefaultHttpClient(ccm);
HttpGet getMethod = new HttpGet(new HttpGet("https://app.test.com/testing/rest/authentication-point/authentication"),context);
HttpResponse response = httpClient.execute(getMethod);
int statusCode = response.getStatusLine().getStatusCode();
推荐阅读
- middleware - GET 以外的代理请求与 NestJS 和 http-proxy-middleware 挂起
- ruby-on-rails - Rspec rubocop错误`更喜欢使用验证双打而不是普通双打'
- java - OpenCV Grabcut 仅输出标记为 GC_FGD 的内容
- openlayers - OpenLayers - 使用 ol/control/Control 比定义定制控件有什么好处?
- c++ - 传递带有隐式参数 C++ 的函数指针
- docker - docker [Errno 2] ip6tables v1.6.1: can't initialize ip6tables table `filter': Table does not exist (你需要insmod吗?)
- c# - 计算一段时间内每小时的每小时分钟数
- android - Android_ID (SSAID) 用于跟踪 Android 10 及更高版本的设备
- android - Flutter Always 后台服务
- c# - 从 UWP 应用程序打印时如何以编程方式设置打印选项?