未验证 提交 e55ffa0b 编写于 作者: O openharmony_ci 提交者: Gitee

!15435 【新增特性】web子系统新增getCertificate接口文档

Merge pull request !15435 from JunMao/master
...@@ -3258,6 +3258,320 @@ struct WebComponent { ...@@ -3258,6 +3258,320 @@ struct WebComponent {
} }
``` ```
### getCertificate<sup>10+</sup>
getCertificate(): Promise<Array<cert.X509Cert>>
获取当前网站的证书信息。使用web组件加载https网站,会进行SSL证书校验,该接口会通过Promise异步返回当前网站的X509格式证书(X509Cert证书类型定义见[X509Cert定义](./js-apis-cert.md)),便于开发者展示网站证书信息。
**系统能力:** SystemCapability.Web.Webview.Core
**返回值:**
| 类型 | 说明 |
| ---------- | --------------------------------------------- |
| Promise<Array<cert.X509Cert>> | Promise实例,用于获取当前加载的https网站的X509格式证书数组。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](../errorcodes/errorcode-webview.md)
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 17100001 | Init error. The WebviewController must be associated with a Web component. |
**示例:**
```ts
// xxx.ets
import web_webview from '@ohos.web.webview';
function Uint8ArrayToString(dataArray) {
var dataString = ''
for (var i = 0; i < dataArray.length; i++) {
dataString += String.fromCharCode(dataArray[i])
}
return dataString
}
function ParseX509CertInfo(x509CertArray) {
let res: string = 'getCertificate success: len = ' + x509CertArray.length;
for (let i = 0; i < x509CertArray.length; i++) {
res += ', index = ' + i + ', issuer name = '
+ Uint8ArrayToString(x509CertArray[i].getIssuerName().data) + ', subject name = '
+ Uint8ArrayToString(x509CertArray[i].getSubjectName().data) + ', valid start = '
+ x509CertArray[i].getNotBeforeTime()
+ ', valid end = ' + x509CertArray[i].getNotAfterTime()
}
return res
}
@Entry
@Component
struct Index {
// outputStr在UI界面显示调试信息
@State outputStr: string = ''
webviewCtl: web_webview.WebviewController = new web_webview.WebviewController();
build() {
Row() {
Column() {
List({space: 20, initialIndex: 0}) {
ListItem() {
Button() {
Text('load bad ssl')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
// 加载一个过期的证书网站,查看获取到的证书信息
this.webviewCtl.loadUrl('https://expired.badssl.com')
})
.height(50)
}
ListItem() {
Button() {
Text('load example')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
// 加载一个https网站,查看网站的证书信息
this.webviewCtl.loadUrl('https://www.example.com')
})
.height(50)
}
ListItem() {
Button() {
Text('getCertificate Promise')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
try {
this.webviewCtl.getCertificate().then(x509CertArray => {
this.outputStr = ParseX509CertInfo(x509CertArray);
})
} catch (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
}
})
.height(50)
}
ListItem() {
Button() {
Text('getCertificate AsyncCallback')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
try {
this.webviewCtl.getCertificate((error, x509CertArray) => {
if (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
} else {
this.outputStr = ParseX509CertInfo(x509CertArray);
}
})
} catch (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
}
})
.height(50)
}
}
.listDirection(Axis.Horizontal)
.height('10%')
Text(this.outputStr)
.width('100%')
.fontSize(10)
Web({ src: 'https://www.example.com', controller: this.webviewCtl })
.fileAccess(true)
.javaScriptAccess(true)
.domStorageAccess(true)
.onlineImageAccess(true)
.onPageEnd((e) => {
this.outputStr = 'onPageEnd : url = ' + e.url
})
.onSslErrorEventReceive((e) => {
// 忽略ssl证书错误,便于测试一些证书过期的网站,如:https://expired.badssl.com
e.handler.handleConfirm()
})
.width('100%')
.height('70%')
}
.height('100%')
}
}
}
```
### getCertificate<sup>10+</sup>
getCertificate(callback: AsyncCallback<Array<cert.X509Cert>>): void
获取当前网站的证书信息。使用web组件加载https网站,会进行SSL证书校验,该接口会通过AsyncCallback异步返回当前网站的X509格式证书(X509Cert证书类型定义见[X509Cert定义](./js-apis-cert.md)),便于开发者展示网站证书信息。
**系统能力:** SystemCapability.Web.Webview.Core
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------- | ---- | ---------------------------------------- |
| callback | AsyncCallback<Array<cert.X509Cert>> | 是 | 通过AsyncCallback异步返回当前网站的X509格式证书。 |
**错误码:**
以下错误码的详细介绍请参见[webview错误码](../errorcodes/errorcode-webview.md)
| 错误码ID | 错误信息 |
| -------- | ------------------------------------------------------------ |
| 17100001 | Init error. The WebviewController must be associated with a Web compoent. |
**示例:**
```ts
// xxx.ets
import web_webview from '@ohos.web.webview';
function Uint8ArrayToString(dataArray) {
var dataString = ''
for (var i = 0; i < dataArray.length; i++) {
dataString += String.fromCharCode(dataArray[i])
}
return dataString
}
function ParseX509CertInfo(x509CertArray) {
let res: string = 'getCertificate success: len = ' + x509CertArray.length;
for (let i = 0; i < x509CertArray.length; i++) {
res += ', index = ' + i + ', issuer name = '
+ Uint8ArrayToString(x509CertArray[i].getIssuerName().data) + ', subject name = '
+ Uint8ArrayToString(x509CertArray[i].getSubjectName().data) + ', valid start = '
+ x509CertArray[i].getNotBeforeTime()
+ ', valid end = ' + x509CertArray[i].getNotAfterTime()
}
return res
}
@Entry
@Component
struct Index {
// outputStr在UI界面显示调试信息
@State outputStr: string = ''
webviewCtl: web_webview.WebviewController = new web_webview.WebviewController();
build() {
Row() {
Column() {
List({space: 20, initialIndex: 0}) {
ListItem() {
Button() {
Text('load bad ssl')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
// 加载一个过期的证书网站,查看获取到的证书信息
this.webviewCtl.loadUrl('https://expired.badssl.com')
})
.height(50)
}
ListItem() {
Button() {
Text('load example')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
// 加载一个https网站,查看网站的证书信息
this.webviewCtl.loadUrl('https://www.example.com')
})
.height(50)
}
ListItem() {
Button() {
Text('getCertificate Promise')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
try {
this.webviewCtl.getCertificate().then(x509CertArray => {
this.outputStr = ParseX509CertInfo(x509CertArray);
})
} catch (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
}
})
.height(50)
}
ListItem() {
Button() {
Text('getCertificate AsyncCallback')
.fontSize(10)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.onClick(() => {
try {
this.webviewCtl.getCertificate((error, x509CertArray) => {
if (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
} else {
this.outputStr = ParseX509CertInfo(x509CertArray);
}
})
} catch (error) {
this.outputStr = 'getCertificate failed: ' + error.code + ", errMsg: " + error.message;
}
})
.height(50)
}
}
.listDirection(Axis.Horizontal)
.height('10%')
Text(this.outputStr)
.width('100%')
.fontSize(10)
Web({ src: 'https://www.example.com', controller: this.webviewCtl })
.fileAccess(true)
.javaScriptAccess(true)
.domStorageAccess(true)
.onlineImageAccess(true)
.onPageEnd((e) => {
this.outputStr = 'onPageEnd : url = ' + e.url
})
.onSslErrorEventReceive((e) => {
// 忽略ssl证书错误,便于测试一些证书过期的网站,如:https://expired.badssl.com
e.handler.handleConfirm()
})
.width('100%')
.height('70%')
}
.height('100%')
}
}
}
```
## WebCookieManager ## WebCookieManager
通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookieManager实例。 通过WebCookie可以控制Web组件中的cookie的各种行为,其中每个应用中的所有web组件共享一个WebCookieManager实例。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册