gpt4 book ai didi

objective-c - NSUrlSessionTaskDelegate 服务器响应

转载 作者:可可西里 更新时间:2023-11-01 01:37:50 25 4
gpt4 key购买 nike

我正在使用 NSUrlSessionTaskDelegate 将文件上传到服务器,但服务器代码没有在 didCompleteWithError 中返回。这与苹果文档相符:

Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host.

但是有没有其他方法可以得到服务器错误,比如 400 Bad Request 之类的?因为即使我收到错误的请求,现在每个文件上传都是成功的..

最佳答案

像这样的东西应该可以工作。但我没有测试它。它作为答案发布只是因为更简单的代码格式......

- (void)URLSession:(NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
if (!error)
{
NSHTTPURLResponse *response = task.response;

NSLog(@"StatusCode: %d",response.statusCode);

if (response.statusCode != 200) //note that other 2xx codes might be valid
{
//....
}
}
else
{
NSLog(@"Error: %@",error.description);
}
}

关于objective-c - NSUrlSessionTaskDelegate 服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34087204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com