You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下使用Swift的URLSession调用API遇未知错误

Ubuntu下Swift调用API出现NSURLErrorDomain Code=-1错误的排查求助

在Ubuntu系统中使用Swift调用API时,运行代码出现未知运行时错误。通过Wireshark确认请求已正常发送至API,且API也返回了预期数据,但Swift无法正确接收该数据。

运行的Swift代码

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// Defines the possible errors
public enum URLSessionAsyncErrors: Error {
    case invalidUrlResponse, missingResponseData
}

public extension URLSession {
     func asyncData(from url: URL) async throws -> (Data, URLResponse) {
        return try await withCheckedThrowingContinuation { continuation in
            let task = self.dataTask(with: url) { data, response, error in
                if let error = error {
                    continuation.resume(throwing: error)
                    return
                }
                guard let response = response as? HTTPURLResponse else {
                    continuation.resume(throwing: URLSessionAsyncErrors.invalidUrlResponse)
                    return
                }
                guard let data = data else {
                    continuation.resume(throwing: URLSessionAsyncErrors.missingResponseData)
                    return
                }
                continuation.resume(returning: (data, response))
            }
            task.resume()
        }
    }
}

let urlLink = URL(string: "https://trial.serviceobjects.com/AV3/api.svc/GetBestMatchesJson?BusinessName=&Address=27%20E%20Cota%20St&Address2=Suite%20500&City=Santa%20Barbara&State=CA&PostalCode=93101&LicenseKey=ws72-soa1-dev1")
let (data, response) = try await URLSession.shared.asyncData(from: urlLink!)

print(response)
print(data)

错误信息

Swift/ErrorType.swift:200: Fatal error: Error raised at top level: Error Domain=NSURLErrorDomain Code=-1 "(null)" 
Current stack trace:
0    libswiftCore.so                    0x00007f1f4966eb80 _swift_stdlib_reportFatalErrorInFile + 112
1    libswiftCore.so                    0x00007f1f4936043f <unavailable> + 1442879
2    libswiftCore.so                    0x00007f1f49360257 <unavailable> + 1442391
3    libswiftCore.so                    0x00007f1f4935f060 _assertionFailure(_:_:file:line:flags:) + 364
4    libswiftCore.so                    0x00007f1f493bdec6 <unavailable> + 1826502
5    swift                              0x000055c44836ce2b <unavailable> + 20011
6    libswift_Concurrency.so            0x00007f1f498b10ce <unavailable> + 274638
7    libswift_Concurrency.so            0x00007f1f498b1840 swift_job_run + 92
8    libdispatch.so                     0x00007f1f499cdf45 <unavailable> + 192325
9    libdispatch.so                     0x00007f1f499cebbd <unavailable> + 195517
10   libdispatch.so                     0x00007f1f499d6002 <unavailable> + 225282
11   libc.so.6                          0x00007f1f48e94b43 <unavailable> + 609091
12   libc.so.6                          0x00007f1f48f26a00 <unavailable> + 1206784
Illegal instruction (core dumped)

补充说明

  • 已尝试使用标准dataTask函数多次测试,仍得到相同的NSURLErrorDomain Code=-1错误,确认问题并非来自自定义的URLSession扩展。
  • 根据苹果官方文档,Code=-1为未知错误,调试难度极大。
  • 运行环境:Windows 11主机上VirtualBox 7.0.10托管的Ubuntu 22.04.3虚拟机,Swift版本5.8.1。

求问是否有人遇到过此类问题或了解解决方法?

内容的提问来源于stack exchange,提问作者SOReed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:05:54