我在使用 iOS 应用程序时遇到问题(我不是 iOS 开发人员,我负责该应用程序使用的 API)和 DELETE 请求。
Api 对 DELETE 请求使用没有内容的 204 响应,到目前为止,它对所有客户端应用程序都运行良好,没有任何问题。
问题是,当使用 NSUrlConnection 时,所有这些 DELETE 请求要么被处理超过 60 秒,要么由于超时而失败。
此行为仅在 iOS 实现中可见,其他客户端在不到 100 毫秒的时间内收到完全相同请求的响应。
这是常见的行为吗?有谁知道希望不需要重建 API 的任何修复方法?
创建下面的代码只是为了模拟此行为并在 API 开发团队端复制问题,但问题是相同的(当然,访问 token 被遮盖了,身份验证工作正常):
//
// ViewController.m
// NoteablesTest
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *loadingLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_loadingLabel setHidden:true];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)didButtonTouched:(id)sender {
NSString *url = @"https://api.noteables.com/editing-session/c180af93-ad3a-4751-a96a-dc47ff7732d4";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"DELETE"];
[request setValue:@"Bearer XXXX" forHTTPHeaderField:@"Authorization"];
[request setValue:@"Noteables/1.0 (iPhone; iOS 8.1.3; Scale/2.00)" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"application/vnd.api.v1+json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"en;q=1" forHTTPHeaderField:@"Accept-Language"];
NSDate *start = [NSDate date];
[_loadingLabel setHidden:false];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
double timePassed = [start timeIntervalSinceNow];
NSString *message = [NSString stringWithFormat:@"Elapsed: %f seconds", timePassed];
[_loadingLabel setHidden:true];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result!" message: message delegate:self cancelButtonTitle:@"Am I satisfied with this?" otherButtonTitles:nil, nil];
[alert show];
NSLog(@"%@", response);
}];
}
@end
最佳答案
尝试将您的 content-length
header 设置为 0。
关于ios - 具有 204 响应且 Objective-C 中没有内容的 DELETE 请求的巨大延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28684209/