- 版本:2.2.0
- GitHub: https://github.com/gethuman/nativescript-https
- NPM: https://npmjs.net.cn/package/nativescript-https-thelonecabbage
- 下载
- 昨日:0
- 上周:5
- 上个月:17
NativeScript-HTTPS
在Nativescript中调用基于HTTP的API的终极方式。
轻松集成最可靠的本地网络库,同时使用最新和最强大的HTTPS安全功能。
插件版本2.0.0将iOS上的
AFNetworking
升级到4.0.0,不再依赖于UIWebView
。请确保运行pod repo update
以获取您开发机器上最新的AFNetworking
pod。
默认http模块的替代品。
特性
- 现代TLS & SSL安全功能
- 共享连接池减少请求延迟
- 静默恢复常见连接问题
- 所有操作都在本地后台线程上运行
- 透明GZIP
- 支持HTTP/2
- 多部分
- 缓存
- 基本Cookie支持
常见问题解答
SSL锚定是什么?以及所有这些安全术语是什么意思?
我必须使用SSL锚定吗?
不。 此插件默认情况下无需任何安全配置即可运行。无论哪种方式,您都将从上面列出的所有功能中受益。
演示
git clone https://github.com/EddyVerbruggen/nativescript-https
cd nativescript-https/src
npm run demo.ios
npm run demo.android
安装
将tns-platform-declarations
添加到Android和iOS的references.d.ts
!
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
我们还建议将"skipLibCheck": true,
添加到您的tsconfig.json
中。更多信息请参阅此处。
安装插件
tns plugin add nativescript-https
示例
使用GET
方法调用API
import * as Https from 'nativescript-https'
Https.request({
url: 'https://httpbin.org/get',
method: 'GET',
timeout: 30 // seconds (default 10)
}).then(function(response) {
console.log('Https.request response', response)
}).catch(function(error) {
console.error('Https.request error', error)
})
配置
安装您的SSL证书
在项目app
文件夹中创建一个名为assets
的文件夹,例如<project>/app/assets
。使用chrome,访问SSL证书所在的URL。查看详细信息,然后将证书图像拖放到assets
文件夹中。
启用SSL锚定
import { knownFolders } from 'file-system'
import * as Https from 'nativescript-https'
let dir = knownFolders.currentApp().getFolder('assets')
let certificate = dir.getFile('httpbin.org.cer').path
Https.enableSSLPinning({ host: 'httpbin.org', certificate })
一旦启用了SSL锚定,您就无法使用不同的host
或certificate
文件重新启用。
禁用SSL锚定
import * as Https from 'nativescript-https'
Https.disableSSLPinning()
调用此方法后的所有请求将不再使用SSL锚定,直到再次启用。
useLegacy
有一个名为useLegacy
的新选项。您可以为每个请求设置选项。使用该选项时,请求将表现得更像{n} http模块。
- 请求返回的
content
不是一个结果字符串,而是一个对象。大部分遵循HTTPContent格式。您可以调用toJSON
或toFile
。唯一的不同之处在于toFile
返回一个Promise<File>
,这意味着它是异步的,在后台线程中运行! - 错误也返回一个
content
,允许您读取其内容。
Cookie
默认情况下,基本Cookie支持已启用,以在{n} http
模块中工作。将来将添加更多选项。
启用缓存
import { knownFolders, path } from '@nativescript/core/file-system';
import * as Https from 'nativescript-https'
Https.setCache({
diskLocation: path.join(knownFolders.documents().path, 'httpcache'),
diskSize: 10 * 1024 * 1024 // 10 MiB
});
/// later on when calling your request you can use the cachePolicy option
多部分表单数据
如果您将Content-Type
头设置为"multipart/form-data"
,则请求数据将作为多部分表单数据评估。每个请求数据期望的格式如下
{
data: any
parameterName: string,
fileName?: string
contentType?: string
}
如果设置了fileName
和contentType
,则数据期望为iOS上的NSData
或Android上的native.Array
。
选项
export interface HttpsSSLPinningOptions {
host: string
certificate: string
allowInvalidCertificates?: boolean
validatesDomainName?: boolean
commonName?: string
}
import { HttpRequestOptions } from 'tns-core-modules/http';
export interface HttpsRequestOptions extends HTTPOptions{
useLegacy?: boolean
cachePolicy?: 'noCache' | 'onlyCache' | 'ignoreCache'
onProgress?: (current: number, total: number) => void
}
SSLPinning选项 | 描述 |
---|---|
host: 字符串 |
这必须是请求域名,例如sales.company.org 。 |
commonName?: 字符串 |
默认值:options.host。如果证书CN与主机不同(例如*.company.org ),则设置此值(Android特定)。 |
certificate: 字符串 |
您的.cer 证书文件的uri路径。 |
allowInvalidCertificates?: 布尔值 |
默认值:false 。如果您使用SSL pinning,则应始终将此设置为false 。如果您使用自签名证书,则将其设置为true 。 |
validatesDomainName?: 布尔值 |
默认值:true 。确定是否应使用您的已固定证书验证域名。 |
请求数据选项 | 描述 |
---|---|
useLegacy?: 布尔值 |
默认值:false 。[仅iOS]设置为true,以便在content 中直接获取响应数据(当状态码≥300时),而不是在response.body.content 中。 |
`cachePolicy?: 'noCache' | 'onlyCache' |
onProgress?: (current: number, total: number) => void |
[仅iOS]设置进度回调。 |
Webpack / 打包
由于您可能正在将证书与您的应用程序一起分发(如我们的演示所示),请确保它也被Webpack打包。您可以通过使用CopyWebpackPlugin
添加证书(s)来完成此操作。
iOS
故障排除
在开始beef之前,请先了解iOS的App Transport Security。
如果您尝试访问未添加到App Transport Security白名单的https
路由,则它将不会工作!您可以通过将以下内容添加到项目的Info.plist
中来绕过此行为:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
此插件不会自动将
NSAllowsArbitraryLoads
添加到您的项目的Info.plist
中。
Android
故障排除
如果您的应用程序在主线程上执行过多网络操作时崩溃,则请将allowLargeResponse
选项的值设置为true
传递给request
函数。
感谢
谁 | 为什么 |
---|---|
罗伯特·拉维蒂 | 在将其转让给我之前,罗伯特·拉维蒂长期创建和维护此插件,在杰夫·惠普利的帮助下。 |
AFNetworking | AFNetworking 一个令人愉快的iOS、OS X、watchOS和tvOS的网络框架。 |
Square | okhttp 用于Android和Java应用程序的HTTP+HTTP/2客户端。 |