我正在建立一个电子商务网站,我打算使用的其中一个托运人提供了一个API,该API可用于自动下达订单和跟踪订单。甚至以为他们有几种语言的客户(PHP,Python,Ruby,Node),但他们还没有Java语言,因此我决定编写自己的语言。我正在尝试对其进行测试,但我不断收到此错误
Hostname in certificate didn't match: <api.theprintful.com> != <behappy.me> OR <behappy.me> OR <www.behappy.me>
我已经按照他们对文档的指示进行操作:
Some API requests (like the Product catalog and Country codes) are available without an
API key, but for majority of requests you will need to authenticate your store.
...
To perform authorization, you need to add the Authorization header with a Base64 encoded
API key when performing a request. So if the API key is vv0gtldg-1d7v-qclq:e2vv-
lmhg676ak0z1, then you need to add this header to the API request:
Authorization: Basic dnYwZ3RsZGctMWQ3di1xY2xxOmUydnYtbG1oZzY3NmFrMHox
我已经按照指示
//constructor that is used to make the object
public ProductsRequest(String apiKey)
{
super();
this.apiKey = apiKey;
this.encodedApiKey = codec.encodeBase64String(apiKey.getBytes());
}
//function that is used to call the api
public List<Product> getAllProductList(String path)
{
//the list of products that will be returned
List<Product> returnedProducts = null;
//set the endpoint that will be used to get the list of products
httpGet = new HttpGet(path);
String basicString = "Basic "+this.encodedApiKey;
httpGet.addHeader("Authorization",basicString.substring(0,basicString.length()-2));
try
{
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200)
{
entity = response.getEntity();
jsonResponse = EntityUtils.toString(entity);
Type listType = new TypeToken<List<Product>>(){}.getType();
JsonNode root = mapper.readTree(jsonResponse);
ArrayNode products = (ArrayNode) root.path("result");
returnedProducts = (List<Product>) gson.fromJson(products.toString(),listType);
}
}
catch (IOException e)
{
e.printStackTrace();
}
return returnedProducts;
}
我不知道为什么会出现此错误。我尝试通过电子邮件向公司的开发团队发送电子邮件,他们建议我尝试创建一个假商店,该商店不与我可以用于测试的任何电子商务软件连接,但他们表示一切看上去都很好。我尝试遵循该建议,但仍然遇到相同的错误。
我在Stackoverflow上发现了这个线程,看起来好像有人遇到了类似的问题。我只是想知道为什么当其他语言的其他客户端库都不需要经过此过程时,为什么我必须用Java来做到这一点?
据我所知,我正在按照文档说明的方式对所有内容进行编码,并且正确设置了标头。我需要使用其他库吗?
我目前正在使用apache commons编解码器
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
和apache http-components
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
我尝试从公司使用另一种语言的客户端库之一运行示例,并且一切正常。我知道我的代码有问题,而不是其SSL证书有问题,但我不知道这是什么。
您的服务器正在使用“服务器名称指示”来显示不同的证书,具体取决于您要求的名称(这是在同一IP地址和端口上托管带有SSL / TLS的多个证书所必需的)。
您可以使用以下方法进行检查:
openssl s_client -connect api.theprintful.com:443 -servername api.theprintful.com
和
openssl s_client -connect api.theprintful.com:443
SNI当前仅从Java 7开始可用(并且仅在客户端,已计划为Java 8提供服务器端支持)。
使用时,执行Java 7的应用程序应自动为您执行此操作HttpsURLConnection
。
Apache HTTP Client中对此的修复是最新的。您应该确保确实使用版本4.3.2和Java7。版本4.3.1(甚至Java 7)或Java 6的4.3.2也会失败:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://sni.velox.ch/");
HttpResponse response = httpClient.execute(httpGet);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句