ios objective c : 如何从服务器获取 return Content-Length(http header) 的 NSURLSession
ios objective c : How to get NSURLSession to return Content-Length(http header) from server
我已经试过了
- (long long) getContentLength
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"HEAD";
[request addValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
NSURLSessionDownloadTask *uploadTask
= [session downloadTaskWithRequest:request
completionHandler:^(NSURL *url,NSURLResponse *response,NSError *error) {
NSLog(@"handler size: %lld", response.expectedContentLength);
totalContentFileLength = [[NSNumber alloc] initWithFloat:response.expectedContentLength].longLongValue;
}];
NSLog(@"content length=%lld", totalContentFileLength);
[uploadTask resume];
return totalContentFileLength;
}
我总是从 return 值中得到 0
。
这是因为您在 completion handler
之外 returning 函数值,所以您的函数在得到服务器响应之前是 returns 值。现在您不能 return 来自 completion handler
的值。因此,您需要创建具有自定义 completion handler
作为参数的方法,例如
- (void) getContentLength : (void(^)(long long returnValue))completionHandler
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"HEAD";
[request addValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
NSURLSessionDownloadTask *uploadTask
= [session downloadTaskWithRequest:request
completionHandler:^(NSURL *url,NSURLResponse *response,NSError *error) {
NSLog(@"handler size: %lld", response.expectedContentLength);
totalContentFileLength = [[NSNumber alloc] initWithFloat:response.expectedContentLength].longLongValue;
completionHandler(totalContentFileLength);
}];
NSLog(@"content length=%lld", totalContentFileLength);
[uploadTask resume];
}
你可以这样调用这个方法,
[self getContentLength:^(long long returnValue) {
NSLog(@"your content length : %lld",returnValue);
}];
对于 swift 3.0:
(使用普通的旧式函数..)
func getContentLengthOf(urlString: String,
completionHandler: @escaping (Int64?) -> () ) {
let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.httpMethod = "HEAD"
request.addValue( "identity", forHTTPHeaderField: "Accept-Encoding")
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
let contentLength : Int64 = response.expectedContentLength
completionHandler(contentLength)
} else {
completionHandler(nil)
}
})
task.resume()
}
并这样调用:
....
let URLString = "https://lh4.googleusercontent.com/-KdgJnz1HIdQ/AAAAAAAAAAI/AAAAAAAAA8s/31PBnNCL-qs/s0-c-k-no-ns/photo.jpg"
getContentLengthOf(urlString: URLString) { (contentLength: Int64?) in
if let contentLength = contentLength {
print("size is: \(contentLength)")
}else{
print("error getting contentLength")
}
}
在回调中你会得到一个可选的 Int64。
我已经试过了
- (long long) getContentLength
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"HEAD";
[request addValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
NSURLSessionDownloadTask *uploadTask
= [session downloadTaskWithRequest:request
completionHandler:^(NSURL *url,NSURLResponse *response,NSError *error) {
NSLog(@"handler size: %lld", response.expectedContentLength);
totalContentFileLength = [[NSNumber alloc] initWithFloat:response.expectedContentLength].longLongValue;
}];
NSLog(@"content length=%lld", totalContentFileLength);
[uploadTask resume];
return totalContentFileLength;
}
我总是从 return 值中得到 0
。
这是因为您在 completion handler
之外 returning 函数值,所以您的函数在得到服务器响应之前是 returns 值。现在您不能 return 来自 completion handler
的值。因此,您需要创建具有自定义 completion handler
作为参数的方法,例如
- (void) getContentLength : (void(^)(long long returnValue))completionHandler
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
request.HTTPMethod = @"HEAD";
[request addValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];
NSURLSessionDownloadTask *uploadTask
= [session downloadTaskWithRequest:request
completionHandler:^(NSURL *url,NSURLResponse *response,NSError *error) {
NSLog(@"handler size: %lld", response.expectedContentLength);
totalContentFileLength = [[NSNumber alloc] initWithFloat:response.expectedContentLength].longLongValue;
completionHandler(totalContentFileLength);
}];
NSLog(@"content length=%lld", totalContentFileLength);
[uploadTask resume];
}
你可以这样调用这个方法,
[self getContentLength:^(long long returnValue) {
NSLog(@"your content length : %lld",returnValue);
}];
对于 swift 3.0: (使用普通的旧式函数..)
func getContentLengthOf(urlString: String,
completionHandler: @escaping (Int64?) -> () ) {
let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.httpMethod = "HEAD"
request.addValue( "identity", forHTTPHeaderField: "Accept-Encoding")
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
let contentLength : Int64 = response.expectedContentLength
completionHandler(contentLength)
} else {
completionHandler(nil)
}
})
task.resume()
}
并这样调用:
....
let URLString = "https://lh4.googleusercontent.com/-KdgJnz1HIdQ/AAAAAAAAAAI/AAAAAAAAA8s/31PBnNCL-qs/s0-c-k-no-ns/photo.jpg"
getContentLengthOf(urlString: URLString) { (contentLength: Int64?) in
if let contentLength = contentLength {
print("size is: \(contentLength)")
}else{
print("error getting contentLength")
}
}
在回调中你会得到一个可选的 Int64。