@interface MySessionDelegate : NSObject <NSURLSessionDelegate> @end @implementation MySessionDelegate - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics { // 打印出请求的各项指标 for(NSURLSessionTaskTransactionMetrics *transaction in metrics.transactionMetrics) { NSHTTPURLResponse *response = (NSHTTPURLResponse *)transaction.response; NSLog(@"Request URL: %@", transaction.request.URL); NSLog(@"HTTP Status Code: %ld", (long)response.statusCode); NSLog(@"Request Start Time: %@", transaction.startDate); NSLog(@"Request End Time: %@", transaction.endDate); NSLog(@"Request Duration: %f", [transaction.duration doubleValue]); NSLog(@"Bytes Sent: %lld", transaction.countOfRequestBodyBytesSent); NSLog(@"Bytes Received: %lld", transaction.countOfResponseBodyBytesReceived); NSLog(@"Redirection Count: %lu", (unsigned long)transaction.redirectCount); NSLog(@"Request Method: %@", transaction.request.HTTPMethod); } } @end
@interface MyURLProtocol : NSURLProtocol @end @interface MyURLProtocol() @property(nonatomic, strong) NSURLConnection *connection; @property(atomic, strong) NSMutableData *receivedData; @end @implementation MyURLProtocol + (BOOL)canInitWithRequest:(NSURLRequest *)request { if ([NSURLProtocol propertyForKey:kCustomURLProtocolKey inRequest:request]) { return NO; } #if defined(Debug) return YES; #elif defined(Release) return NO; #endif } + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; } - (void)startLoading { NSMutableURLRequest *mutableRequest = [[self request] mutableCopy]; [NSURLProtocol setProperty:@YES forKey:kCustomURLProtocolKey inRequest:mutableRequest]; self.connection = [NSURLConnection connectionWithRequest:mutableRequest delegate:self]; } - (void)stopLoading { [self.connection cancel]; self.connection = nil; } #pragma mark - NSURLConnection Delegate #pragma mark - NSURLConnection Delegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [[self client] URLProtocol:self didLoadData:data]; if (self.receivedData == nil) { self.receivedData = [[NSMutableData alloc] init]; } [self.receivedData appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSURLRequest *logRequest = [self.request copy]; NSData *data = [self.receivedData copy]; [[self client] URLProtocolDidFinishLoading:self]; self.connection = nil; #if defined(Debug) dispatch_async(dispatch_get_main_queue(), ^{ [[LogManager sharedInstance] addNetworkLog:logRequest response:data]; }); #else dispatch_async(dispatch_get_main_queue(), ^{ [[LogManager sharedInstance] addNetworkLog:logRequest response:data]; }); #endif } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [[self client] URLProtocol:self didFailWithError:error]; self.connection = nil; } @end