簡體   English   中英

Send ZIP file to a REST based API which is using SSL TLS (https) hosted on AWS from a flutter based mobile application

[英]Send ZIP file to a REST based API which is using SSL TLS (https) hosted on AWS from a flutter based mobile application

之前我創建了一個 REST API ,它采用 ZIP 文件和其他兩個參數作為來自 Z5ACEBC4CB70DDBB0AEZ4B0AC76 的輸入

var uri = Uri.parse("http://XX.XXX.XX.XX/gatewayapp/userinfo/sendFileToServer/");
var request = http.MultipartRequest('POST', uri);
     Map<String, String> headers = {
       "Accept": "application/json"
     };
     //add headers
     request.headers.addAll(headers);
     request.fields["customerNumber"] = customerNumber;
     request.fields['zippassword'] = password;
   var file=  await http.MultipartFile.fromPath(
         'userDetailsFile',
         filePath
     );
     request.files.add(file);
     
     await request.send().then((streamedResponse) async {
     ....Implemented the business logic on the response which I was getting
     }

它按預期工作。

We moved the API from HTTP to HTTPS using Nginx on the AWS server and changed all the GET and POST-call from the mobile application using HTTPClient and HttpClientRequest and it worked as per the expectation.

但是,我們無法在 API 上使用 HTTPClient 和 HttpClientRequest 執行多部分請求。 我嘗試使用 httpclient 方法,但沒有運氣。 我還嘗試了以下鏈接中給出的內容:

在 Dart 上使用 HTTPClient 語言 github

var uri = Uri.parse("https://XX.XXX.XX.XX/gatewayapp/userinfo/sendFileToServer/");    
HttpClient client = await CommonController.createHttpClient();
HttpClientRequest request = await client.post( uri.toString() , 443, filePath);

誰能幫助我朝着正確的方向前進? 任何幫助,將不勝感激! 謝謝

很抱歉這么晚才回答這個問題。

我能夠使用 flutter 提供的 HTTPClient 使用 SSL (部署在 AWS 上的自簽名證書)發送 ZIP 文件。

問題

  1. 當我們使用 Open SSL 生成證書並將這些證書添加到 NGINX 配置中以啟用 SSL 時。 它沒有root權限。)

  2. 通過在HTTPClientRequest中添加 SSL 證書,我們能夠達到第一個 HTTPS 請求,如下面的代碼所示。 但在隨后的 HTTP 請求中出現錯誤。

     static Future createHttpClient(//Passing the parameters) async { SecurityContext securityContext = SecurityContext.defaultContext; var certificate = (await rootBundle.load(Constants.HTTPS_CRT_PATH)).buffer.asInt8List(); var key = (await rootBundle.load(Constants.HTTPS_KEY_PATH)).buffer.asInt8List(); securityContext.useCertificateChainBytes(certificate); securityContext.usePrivateKeyBytes(key); HttpClient httpClient = new HttpClient(context: securityContext); httpClient.badCertificateCallback = ((X509Certificate cert, String host, int port) => true); HttpClientRequest request = httpClient.postUrl(Uri.parse(url)); request.contentLength = json.encode(requestBody).length; request.write(json.encode(requestBody)); HttpClientResponse response = await request.close(); print("response status code "+response.statusCode.toString()); return response; }
  3. 每次我們發出 HTTP 請求並收到錯誤 SSL 握手異常時,我們都會添加證書:

     I/flutter ( 9066): HandshakeException: Handshake error in client (OS Error: I/flutter ( 9066): CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:354))

解決問題的步驟

  1. 在系統的/etc/ssl/文件夾中添加證書,因為證書需要 root 訪問權限。
  2. 其次,當我們發出第一個 HTTP 請求時,我們需要使用 Singleton 設計模式添加證書一次。
  3. 第三,在進行任何 HTTP 調用時,我們只需要使用 HTTP Client 的 Multipart 方法來發送請求。

我將添加代碼示例以使用 Singleton 模式添加證書並進行 HTTP 調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM