简体   繁体   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

Previously I created a REST API which was taking a ZIP file and two other parameters as input from the flutter 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
     }

It was working as per the expectation.它按预期工作。

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. 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.

However, we are not able to do a multipart request using HTTPClient and HttpClientRequest on the API.但是,我们无法在 API 上使用 HTTPClient 和 HttpClientRequest 执行多部分请求。 I tried using httpclient method but no luck.我尝试使用 httpclient 方法,但没有运气。 I also tried something which is given on the below link:我还尝试了以下链接中给出的内容:

Using HTTPClient on Dart lang github 在 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);

Can anyone please help me move forward in the right direction?谁能帮助我朝着正确的方向前进? Any help would be appreciated!任何帮助,将不胜感激! Thanks谢谢

Apologies for the answering the question so late.很抱歉这么晚才回答这个问题。

I was able to send ZIP file using SSL ( Self Signed certificate deployed on AWS ) using the HTTPClient provided by flutter.我能够使用 flutter 提供的 HTTPClient 使用 SSL (部署在 AWS 上的自签名证书)发送 ZIP 文件。

Issue问题

  1. When we generated certificate using Open SSL and added those certificates in the NGINX configuration to enable SSL.当我们使用 Open SSL 生成证书并将这些证书添加到 NGINX 配置中以启用 SSL 时。 ( It was not having root permission .) 它没有root权限。)

  2. We were able to hit the first HTTPS request by adding SSL certificate in the HTTPClientRequest as show in below code.通过在HTTPClientRequest中添加 SSL 证书,我们能够达到第一个 HTTPS 请求,如下面的代码所示。 But were getting error in the sub sequent HTTP request.但在随后的 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. We were adding the certificate every time we were making a HTTP request and getting a error of Bad SSL Handshake exception:每次我们发出 HTTP 请求并收到错误 SSL 握手异常时,我们都会添加证书:

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

Steps to fix the issue解决问题的步骤

  1. Add the certificate in the /etc/ssl/ folder of the system as the certificate requires root access.在系统的/etc/ssl/文件夹中添加证书,因为证书需要 root 访问权限。
  2. Second we need to add certificate using Singleton Design pattern just once when we are making the first HTTP request.其次,当我们发出第一个 HTTP 请求时,我们需要使用 Singleton 设计模式添加证书一次。
  3. Third, when making any HTTP call we just need to use Multipart method of HTTP Client to send the request.第三,在进行任何 HTTP 调用时,我们只需要使用 HTTP Client 的 Multipart 方法来发送请求。

I will add the code sample for adding the certificate using Singleton pattern and making the HTTP calls.我将添加代码示例以使用 Singleton 模式添加证书并进行 HTTP 调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 flutter 移动应用程序连接到 rest API 中托管在 Z3115FB34DFCB5BA87AA049707C596 中的节点 js 中? - How to connect a flutter mobile app to a rest API in node js which is hosted in heroku? Flutter - 基于 XML 文件的动态页面设计(通过 REST API) - Flutter - Dynamic page design based on a XML file (over a REST API) Flutter rest api https - Flutter rest api https calling 如何将类型文件的图像(从颤振移动应用程序)发送到 php 数据库 - How to send Image of type file(From flutter mobile application ) to php database 如何根据 Flutter 中的 DropdownMenuItem 选定值显示 REST API 数据? - How to show REST API data based on DropdownMenuItem selected value in flutter? 使用 Flutter 的移动应用程序 - Mobile Application using Flutter 当我使用图像选择器从画廊中选择图像时,颤动图像上传将使用颤振和 laravel REST API 发送 - Flutter image upload when I select image from galery using image picker it will send using flutter and laravel REST API 可以将 Django 网站移植为基于 Flutter 的移动应用程序,而无需 Django Z50780F47F6839D047D60BCZF455EE - Can Django website be ported as Flutter based mobile app without Django REST APIs? Burp Suit 没有拦截来自 Flutter iOS 移动应用程序的 api 调用 - Burp Suit not intercepting api calls from Flutter iOS mobile application Flutter 下载音频 zip 文件并在应用程序中播放 - Flutter download audio zip file and play in application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM