NSURLConnection 无法连接到远程 IP
NSURLConnection cannot connect to remote IP
我有一个 SpringBoot 应用程序 运行 在远程机器上的 docker 容器中(它实际上是我的 Mac 旁边的一个小盒子,但无论如何...... ).我还有一个实例 运行 作为 SpringBoot 应用程序,在我的 Mac 上本地使用 bootRun。这些实例没有以任何方式连接。我有一个小型 iOS 测试应用程序,用于接受推送通知,该通知会向此 SB 应用程序公开的端点发出 REST 调用。我有一个 iPhone 连接的物理设备,Xcode 将应用程序安装到该设备上。
如果我使用 Mac 的 IP 地址将 iOS 应用程序 REST 调用指向本地 运行 bootRun 实例,则调用工作正常并注册,一切都很好。但是,如果我将 iOS REST 调用指向我的远程机器 运行 Docker 实例,调用将失败。但是,如果我从终端执行 curl
命令,使用相同的 URL 和负载,它工作正常。所以不存在防火墙问题或类似问题。报错是:
2017-10-04 14:16:18.907472+0100 IosNotificationApp[563:28094] TIC TCP Conn Failed [1:0x1c416a080]: 1:61 Err(61)
2017-10-04 14:16:18.907716+0100 IosNotificationApp[563:28094] Task <7111CDD1-CB81-410E-B928-AE46E6964184>.<0> HTTP load failed (error code: -1004 [1:61])
2017-10-04 14:16:18.908426+0100 IosNotificationApp[563:28093] NSURLConnection finished with error - code -1004
2017-10-04 14:16:18.918332+0100 IosNotificationApp[563:28048] Received response: (null), data: (null)
2017-10-04 14:16:18.918822+0100 IosNotificationApp[563:28048] Failed to register.: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x1c0252330 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "Could not connect to the server." UserInfo={NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=Could not connect to the server.}}, NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}
这是我的 didRegisterForRemoteNotificationsWithDeviceToken
函数:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device token is: %@", newToken);
NSString *urlString = [NSString stringWithFormat:@"http://111.222.3.4:1234/ios-notification-server/register"];
NSString *body =[NSString stringWithFormat:@"{\"foo\":\"bar\",\"deviceToken\":\"%@\"}", newToken];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Bearer custom.token" forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlString]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *connectionError) {
NSLog(@"Received response: %@, data: %@", response, data);
if (data.length > 0 && connectionError == nil) {
NSLog(@"Successfully registered");
} else {
NSLog(@"Failed to register.: %@", connectionError);
}
}] resume];
}
远程服务器上没有任何注册为身份验证失败或其他任何内容,它似乎根本没有发出调用。有谁知道为什么 Xcode 在调用不是机器本地分配的 IP 时会导致这种情况发生? Xcode 是否有任何内置功能可以帮助我诊断正在发生的事情?
对于物理设备,请转至 System Preferences -> Sharing
。单击 Internet Sharing
,并启用 To computers using: iPhone USB
。
对于 iPad,我假设您会启用 iPad USB
。
我有一个 SpringBoot 应用程序 运行 在远程机器上的 docker 容器中(它实际上是我的 Mac 旁边的一个小盒子,但无论如何...... ).我还有一个实例 运行 作为 SpringBoot 应用程序,在我的 Mac 上本地使用 bootRun。这些实例没有以任何方式连接。我有一个小型 iOS 测试应用程序,用于接受推送通知,该通知会向此 SB 应用程序公开的端点发出 REST 调用。我有一个 iPhone 连接的物理设备,Xcode 将应用程序安装到该设备上。
如果我使用 Mac 的 IP 地址将 iOS 应用程序 REST 调用指向本地 运行 bootRun 实例,则调用工作正常并注册,一切都很好。但是,如果我将 iOS REST 调用指向我的远程机器 运行 Docker 实例,调用将失败。但是,如果我从终端执行 curl
命令,使用相同的 URL 和负载,它工作正常。所以不存在防火墙问题或类似问题。报错是:
2017-10-04 14:16:18.907472+0100 IosNotificationApp[563:28094] TIC TCP Conn Failed [1:0x1c416a080]: 1:61 Err(61)
2017-10-04 14:16:18.907716+0100 IosNotificationApp[563:28094] Task <7111CDD1-CB81-410E-B928-AE46E6964184>.<0> HTTP load failed (error code: -1004 [1:61])
2017-10-04 14:16:18.908426+0100 IosNotificationApp[563:28093] NSURLConnection finished with error - code -1004
2017-10-04 14:16:18.918332+0100 IosNotificationApp[563:28048] Received response: (null), data: (null)
2017-10-04 14:16:18.918822+0100 IosNotificationApp[563:28048] Failed to register.: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x1c0252330 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "Could not connect to the server." UserInfo={NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1, NSLocalizedDescription=Could not connect to the server.}}, NSErrorFailingURLStringKey=http://111.222.3.4:1234/ios-notification-server/register, NSErrorFailingURLKey=http://111.222.3.4:1234/ios-notification-server/register, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}
这是我的 didRegisterForRemoteNotificationsWithDeviceToken
函数:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device token is: %@", newToken);
NSString *urlString = [NSString stringWithFormat:@"http://111.222.3.4:1234/ios-notification-server/register"];
NSString *body =[NSString stringWithFormat:@"{\"foo\":\"bar\",\"deviceToken\":\"%@\"}", newToken];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Bearer custom.token" forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlString]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *connectionError) {
NSLog(@"Received response: %@, data: %@", response, data);
if (data.length > 0 && connectionError == nil) {
NSLog(@"Successfully registered");
} else {
NSLog(@"Failed to register.: %@", connectionError);
}
}] resume];
}
远程服务器上没有任何注册为身份验证失败或其他任何内容,它似乎根本没有发出调用。有谁知道为什么 Xcode 在调用不是机器本地分配的 IP 时会导致这种情况发生? Xcode 是否有任何内置功能可以帮助我诊断正在发生的事情?
对于物理设备,请转至 System Preferences -> Sharing
。单击 Internet Sharing
,并启用 To computers using: iPhone USB
。
对于 iPad,我假设您会启用 iPad USB
。