HttpClient 无法忽略或绕过自签名证书错误
Posted
技术标签:
【中文标题】HttpClient 无法忽略或绕过自签名证书错误【英文标题】:HttpClient cannot ignore or bypass self signed certificate error 【发布时间】:2019-02-23 10:56:51 【问题描述】:我尝试向服务器发送 POST 数据,但收到错误“net::ERR_CERT_INVALID”。如下所示的代码是我试图绕过错误但仍然失败的代码。请帮忙指教。谢谢。
import HttpClient, HttpParams from '@angular/common/http';
createData(data): Promise<any>
let post_message = data;
let header_node =
Accept: 'application/json',
rejectUnauthorized: 'false',
// this.oauth_header.Authorization = 'Bearer ' + access_token;
const url_params = new HttpParams()
.set('rejectUnauthorized', 'false')
.set('requestCert', 'false')
.set('insecure', 'true')
return this.http.post('https://ip/createdata', post_message,
headers: header_node,
).toPromise();
【问题讨论】:
嗨@CBV,你能用角度解决这个错误吗? 【参考方案1】:尝试将标头作为HttpHeaders
传递给POST
方法。
import HttpClient, HttpParams, HttpHeaders from '@angular/common/http';
createData(data): Promise<any>
let post_message = data;
let header_node =
headers: new HttpHeaders(
'Accept': 'application/json' ,
'rejectUnauthorized': 'false' )
;
return this.http.post('https://ip/createdata', post_message, header_node).toPromise();
【讨论】:
以上是关于HttpClient 无法忽略或绕过自签名证书错误的主要内容,如果未能解决你的问题,请参考以下文章
如何忽略 Apache HttpComponents HttpClient 5.1 中的 SSL 证书错误