[英]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问题
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权限。)
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; }
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解决问题的步骤
/etc/ssl/
folder of the system as the certificate requires root access./etc/ssl/
文件夹中添加证书,因为证书需要 root 访问权限。 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.