之前对接的webservice接口都是发布在http上的,今天突然拿到一个接口地址是https的,意味着需要证书验证,利用https模块去请求接口有一个入参可以直接忽略证书,但是nodejs中调用webservice用的是soap模块,看了文档说明发现有一个方法看起来挺像的,下面是官方的例子
ClientSSLSecurity
client.setSecurity(new soap.ClientSSLSecurity(
'/path/to/key',
'path/to/cert',
'/path/to/ca-cert', /*or an array of buffer: [fs.readFileSync('/path/to/ca-cert/1', 'utf8'),
'fs.readFileSync('/path/to/ca-cert/2', 'utf8')], */
{ /*default request options like */
// strictSSL: true,
// rejectUnauthorized: false,
// hostname: 'some-hostname'
// secureOptions: constants.SSL_OP_NO_TLSv1_2,
// forever: true,
},
));
注释:如果运行时候协议有问题可以尝试修改一下参数
- rejectUnauthorized: false
- strictSSL: false
- secureOptions: constants.SSL_OP_NO_TLSv1_2 (this is likely needed for node >= 10.0)
这个方法看起来就是我想要的忽略证书,但是该方法需要先创建一个client,创建client的时候也是走的https,同样是会要证书。
解决办法
将url加上?wsdl放到浏览器,然后把加载出来的wsdl文件另存为xml保存到一个位置,soap模块创建client的时候url用本地wsdl文件就可以,然后再调用上面官方给出的方法,完美解决问题。
client.setSecurity(new soap.ClientSSLSecurity('','','',
{ "strictSSL": false,
"rejectUnauthorized": false,
"forever": true
},
));
前三个参数本来应该书传证书的,但是我这边没有证书,所以就都是空,候面options的几个入参就是忽略证书验证。