尝试使用iPhone14模拟器向服务器发送http post请求,代码片段如下:
var request = URLRequest(url: URL(string: "https://localhost:8443/process_file"))
request.httpMethod = "POST"
var body = Data() // add body data here
let task = URLSession.shared.dataTask(with: request)
{ data, response, error in
// Check for errors
if let error = error {
completion(false, error)
return
}
// Check for a successful response
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
completion(false, nil)
return
}
// The request was successful
completion(true, nil)
}
// Start the URLSession task
task.resume()
我测试了服务器代码使用 dart 客户端发送后的请求,它的作品。使用 Postman 请求也工作。但斯威夫特代码生成以下错误。我试图改变证书没有任何成功。
2023-03-18 16:22:32.467056-0400 MyApp[3511:81183] Connection 1: default TLS Trust
evaluation failed(-9807)
2023-03-18 16:22:32.467811-0400 MyApp[3511:81183] Connection 1: TLS Trust encountered
error 3:-9807
2023-03-18 16:22:32.468325-0400 MyApp[3511:81183] Connection 1: encountered
error(3:-9807)
2023-03-18 16:22:32.484207-0400 MyApp[3511:81183] Task <1C9997DB-4481-42FC-9698-
7C1CE90AF535>.<1> HTTP load failed, 0/0 bytes (error code: -1202 [3:-9807])
2023-03-18 16:22:32.524509-0400 MyApp[3511:81183] Task <1C9997DB-4481-42FC-9698-
7C1CE90AF535>.<1> finished with error [-1202] Error Domain=NSURLErrorDomain
Code=-1202 "The certificate for this server is invalid. You might be connecting to
a server that is pretending to be “localhost” which could put your confidential
information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to
connect to the server anyway?, _kCFStreamErrorDomainKey=3,
NSErrorPeerCertificateChainKey=("<cert(0x7ff70501f200) s: localhost i: MY-CA>"),
NSErrorClientCertificateStateKey=0,
NSErrorFailingURLKey=https://localhost:8443/process_file,
NSErrorFailingURLStringKey=https://localhost:8443/process_file,
NSUnderlyingError=0x60000215d8f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1202 "
(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0,
kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x600001e35540>,
_kCFNetworkCFStreamSSLErrorOriginalValue=-9807, _kCFStreamErrorDomainKey=3,
_kCFStreamErrorCodeKey=-9807, kCFStreamPropertySSLPeerCertificates=(
"<cert(0x7ff70501f200) s: localhost i: MY-CA>"
)}}, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <1C9997DB-4481-42FC-9698-7C1CE90AF535>.<1>"
), _kCFStreamErrorCodeKey=-9807,
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <1C9997DB-4481-42FC-9698-
7C1CE90AF535>.<1>, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef:
0x600001e35540>, NSLocalizedDescription=The certificate for this server is invalid.
You might be connecting to a server that is pretending to be “localhost” which could
put your confidential information at risk.}
Error sending file to server: The certificate for this server is invalid. You might
be connecting to a server that is pretending to be “localhost” which could put your
confidential information at risk.
在macOS 12.6.3上使用Xcode 14.2
1条答案
按热度按时间ngynwnxp1#
参考Apple的应用传输安全性并添加localhost例外
另外记得为开发和生产环境创建单独的Info.plist。或者在上传之前删除这个异常。