1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import common from '@ohos.app.ability.common'; 17import promptAction from '@ohos.promptAction'; 18import request from '@ohos.request'; 19import connection from '@ohos.net.connection'; 20import router from '@ohos.router'; 21import { BusinessError } from '@ohos.base'; 22import { logger, TOAST_BOTTOM } from '@ohos/uploaddownload'; 23 24const TAG: string = 'CertLockSample'; 25 26@Entry 27@Component 28struct CertLock { 29 @State message: string = '' 30 @State taskId: string = ''; 31 @State progress: string = '0%'; 32 33 build() { 34 Navigation() { 35 Text(this.message) 36 .fontSize(16) 37 .fontColor($r('app.color.text_message')) 38 .textAlign(TextAlign.Start) 39 .id('http_response') 40 .width('90%') 41 .height('80%') 42 .maxLines(30) 43 .textOverflow({ overflow: TextOverflow.Ellipsis }) 44 45 Button($r('app.string.upload')) 46 .id('upload') 47 .width('90%') 48 .fontSize(16) 49 .height(40) 50 .margin({ top: 10, bottom: 20 }) 51 .enabled(true) 52 .backgroundColor($r('app.color.button_blue')) 53 .onClick(() => { 54 this.uploadTask(); 55 }) 56 57 Button($r('app.string.download')) 58 .id('download') 59 .type(ButtonType.Capsule) 60 .width('90%') 61 .fontSize(16) 62 .fontColor($r('app.color.btn_text_blue')) 63 .height(40) 64 .margin({ bottom: 10 }) 65 .enabled(true) 66 .backgroundColor($r('app.color.button_light_gray')) 67 .onClick(() => { 68 this.DownloadTask(); 69 }) 70 } 71 .width('100%') 72 .height('100%') 73 .hideBackButton(false) 74 .titleMode(NavigationTitleMode.Mini) 75 .mode(NavigationMode.Stack) 76 .backgroundColor($r('app.color.light_gray')) 77 .hideToolBar(false) 78 .title($r('app.string.cert_lock')) 79 } 80 81 static task: request.agent.Task; 82 static netConnection = connection.createNetConnection(); 83 84 async DownloadTask() { 85 logger.debug(TAG, 'DownloadTask'); 86 let config: request.agent.Config = { 87 action: request.agent.Action.DOWNLOAD, 88 url: 'https://www.baidu.com', 89 gauge: true, 90 overwrite: true, 91 method: 'GET', 92 network: request.agent.Network.ANY, 93 }; 94 let task: request.agent.Task = await request.agent.create(getContext(), config); 95 CertLock.task = task; 96 task.on('progress', async (progress) => { 97 logger.debug(TAG, `/Request download status ${progress.state}, downloaded ${progress.processed}`); 98 promptAction.showToast({ 99 message: $r('app.string.download_finish'), 100 bottom: TOAST_BOTTOM, 101 offset: { dx: 10, dy: 96 } 102 }); 103 this.taskId = task.tid; 104 }) 105 let responseOnCallback = (response: request.agent.HttpResponse) => { 106 logger.debug(TAG, `response : ${response.version} ${response.statusCode} ${response.reason} ${response.headers} ${response.headers.size}`) 107 let headers: string = ''; 108 for (let k of response.headers.keys()) { 109 headers = headers + k + ':' + response.headers.get(k) + '\r\n'; 110 ; 111 logger.debug(TAG, `response 1 ${k}, ${response.headers.get(k)}`) 112 } 113 this.message = headers; 114 }; 115 task.on('response', responseOnCallback); 116 117 task.on('failed', async (progress) => { 118 this.message = JSON.stringify(progress); 119 logger.debug(TAG, `/Request download failed ${(JSON.stringify(progress))}`); 120 promptAction.showToast({ message: $r('app.string.download_fail'), bottom: TOAST_BOTTOM }); 121 }) 122 logger.debug(TAG, 'Downloading before'); 123 await task.start(); 124 logger.debug(TAG, 'Downloading started'); 125 } 126 127 async uploadTask() { 128 logger.debug(TAG, 'uploadTask'); 129 let attachments: Array<request.agent.FormItem> = []; 130 let attachment: request.agent.FormItem = { 131 name: 'test', 132 value: [ 133 { 134 filename: 'testUpload.txt', 135 path: './testUpload.txt', 136 }, 137 ] 138 }; 139 attachments.push(attachment); 140 let config: request.agent.Config = { 141 action: request.agent.Action.UPLOAD, 142 url: 'https://www.baidu.com', 143 title: 'taskOnTest', 144 mode: request.agent.Mode.FOREGROUND, 145 overwrite: true, 146 method: 'POST', 147 data: attachments, 148 saveas: './' 149 }; 150 let createOnCallback = (progress: request.agent.Progress) => { 151 let txt: string = $r('app.string.upload_fail').bundleName; 152 this.progress = txt + ((progress.processed / 1024 / 1024)); 153 promptAction.showToast({ message: $r('app.string.upload_success'), bottom: TOAST_BOTTOM }); 154 }; 155 request.agent.create(getContext(), config).then((task: request.agent.Task) => { 156 task.start((err: BusinessError) => { 157 if (err) { 158 this.progress = `Failed to start the upload task, Code: ${err.code} message: ${err.message}`; 159 return; 160 } 161 }); 162 task.on('progress', createOnCallback); 163 let responseOnCallback = (response: request.agent.HttpResponse) => { 164 logger.debug(TAG, `response : ${response.version} ${response.statusCode} ${response.reason} ${response.headers} ${response.headers.size}`) 165 let headers: string = ''; 166 for (let k of response.headers.keys()) { 167 headers = headers + k + ':' + response.headers.get(k) + '\r\n'; 168 ; 169 logger.debug(TAG, `response 1 ${k}, ${response.headers.get(k)}`) 170 } 171 this.message = headers; 172 }; 173 task.on('response', responseOnCallback); 174 task.on('failed', async (progress) => { 175 this.message = JSON.stringify(progress); 176 logger.debug(TAG, `/Request upload failed ${(JSON.stringify(progress))}`); 177 promptAction.showToast({ message: $r('app.string.upload_fail'), bottom: TOAST_BOTTOM }); 178 }) 179 logger.debug(TAG, `Succeeded in creating a upload task. result: ${task.tid}`); 180 }).catch((err: BusinessError) => { 181 this.progress = `Failed to create a upload task, Code: ${err.code}, message: ${err.message}`; 182 }); 183 } 184 185 aboutToAppear() { 186 logger.debug(TAG, 'aboutToAppear'); 187 } 188 189 stateChange() { 190 logger.debug(TAG, 'stateChange'); 191 } 192}