- (void)printRequestMessage:(NSURLSessionDataTask *)task {
/*
// 請求URL
NSLog(@"請求URL:%@\n",task.originalRequest.URL);
// 請求方式
NSLog(@"請求方式:%@\n",task.originalRequest.HTTPMethod);
// 請求頭信息
NSLog(@"請求頭信息:%@\n",task.originalRequest.allHTTPHeaderFields);
// 請求正文信息
NSLog(@"請求正文信息:%@\n",[[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:NSUTF8StringEncoding]);
*/
// 請求響應時間
NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:self.startRequestDate];
NSLog(@"請求響應時間:%@\n",@(time));
NSLog(@"\n請求URL:%@\n請求方式:%@\n請求頭信息:%@\n請求正文信息:%@\n請求響應時間:%@\n",task.originalRequest.URL,task.originalRequest.HTTPMethod,task.originalRequest.allHTTPHeaderFields,[[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:NSUTF8StringEncoding],@(time));
}
2020-04-26 17:48:23.314223+0800 AnyApp3[47513:992582]
請求URL:http://zhouxunwang.cn/data/?id=75&key=BLnA/NVlS93+h5yA84I3QGjBOwTgsJeZ/px16A&type=top
請求方式:GET
請求頭信息:{
"Accept-Language" = "en;q=1";
"User-Agent" = "AnyApp3/1.0 (iPhone; iOS 13.2.2; Scale/3.00)";
}
請求正文信息:
請求響應時間:4.867936968803406
2020-04-26 17:48:23.314456+0800 AnyApp3[47513:992582] success:???
{"reason":"成功的返回","result":{"stat":"1","data":[{"uniquekey":"39285848ab25baad341d364edc9b3de5","title":"武漢外賣小哥馬路邊吃蛋糕结窘,泣不成聲","date":"2020-04-26 17:20","category":"頭條","author_name":"人民日報","url":"https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200426172055108.html","thumbnail_pic_s":"https:\/\/01imgmini.eastday.com\/mobile\/20200426\/20200426172055_59096a3df3808ba1f7ec6edfa2889ac9_3_mwpm_03200403.jpg","thumbnail_pic_s02":"http:\/\/01imgmini.eastday.com\/mobile\/20200426\/20200426172055_59096a3df3808ba1f7ec6edfa2889ac9_2_mwpm_03200403.jpg","thumbnail_pic_s03":"http:\/\/01imgmini.eastday.com\/mobile\/20200426\/20200426172055_59096a3df3808ba1f7ec6edfa2889ac9_1_mwpm_03200403.jpg"},{"uniquekey":"2d48edd6706e5e0b6b8f513af0e5bf80","title":"全球新冠肺炎COVID-19實時追蹤半等,關注全球疫情動態(tài)","date":"2020-04-26 17:48:18","category":"頭條","author_name":"健康資訊","url":"https:\/\/covid-19.juheapi.c
請求類
#import <Foundation/Foundation.h>
@class SPRequest;
@protocol SPRequestDelegate <NSObject>
- (void)SPRequest:(SPRequest *)request finished:(NSString *)response;
- (void)SPRequest:(SPRequest *)request Error:(NSString *)error;
@end
@interface SPRequest : NSObject
@property (assign) id <SPRequestDelegate> delegate;
/**
*[AFNetWorking]的operationManager對象
*/
@property (nonatomic, strong) AFHTTPSessionManager *operationManager;
/**
*當前的請求operation隊列
*/
@property (nonatomic, strong) NSOperationQueue *operationQueue;
/**
*當前的請求開始時間
*/
@property (nonatomic, strong) NSDate *startRequestDate;
/**
*功能: 創(chuàng)建SPRequest的對象方法
*/
+ (instancetype)request;
/**
*功能:GET請求
*參數:(1)請求的url: urlString
* (2)請求成功調用的Block: success
* (3)請求失敗調用的Block: failure
*/
- (void)GET:(NSString *)URLString
parameters:(NSDictionary*)parameters
success:(void (^)(SPRequest *request, NSString *responseString))success
failure:(void (^)(SPRequest *request, NSError *error))failure;
/**
*功能:POST請求
*參數:(1)請求的url: urlString
* (2)POST請求體參數:parameters
* (3)請求成功調用的Block: success
* (4)請求失敗調用的Block: failure
*/
- (void)POST:(NSString *)URLString
parameters:(NSDictionary*)parameters
success:(void (^)(SPRequest *request, NSString *responseString))success
failure:(void (^)(SPRequest *request, NSError *error))failure;
/**
* post請求
*
* @param URLString 請求網址
* @param parameters 請求參數
*/
- (void)postWithURL:(NSString *)URLString parameters:(NSDictionary *)parameters;
/**
* get 請求
*
* @param URLString 請求網址
*/
- (void)getWithURL:(NSString *)URLString;
/**
*取消當前請求隊列的所有請求
*/
- (void)cancelAllOperations;
@end
#import "SPRequest.h"
@implementation SPRequest
+ (instancetype)request {
return [[self alloc] init];
}
- (instancetype)init {
self = [super init];
if (self) {
self.operationManager = [AFHTTPSessionManager manager];
self.startRequestDate = [NSDate date];
}
return self;
}
- (void)GET:(NSString *)URLString
parameters:(NSDictionary*)parameters
success:(void (^)(SPRequest *request, NSString *responseString))success
failure:(void (^)(SPRequest *request, NSError *error))failure {
self.operationQueue = self.operationManager.operationQueue;
self.operationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[self.operationManager GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"%@",downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 請求結果
NSString *responseJson = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
success(self,responseJson);
// 打印相關Log
[self printRequestMessage:task];
NSLog(@"success:???\n%@",responseJson);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(self,error);
// 打印相關Log
[self printRequestMessage:task];
NSLog(@"failure:???\n%@",error.localizedDescription);
}];
}
- (void)POST:(NSString *)URLString
parameters:(NSDictionary*)parameters
success:(void (^)(SPRequest *request, NSString *responseString))success
failure:(void (^)(SPRequest *request, NSError *error))failure{
self.operationQueue = self.operationManager.operationQueue;
self.operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[self.operationManager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 請求結果
NSString *responseJson = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
success(self,responseJson);
// 打印相關Log
[self printRequestMessage:task];
NSLog(@"success:???\n%@",responseJson);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(self,error);
// 打印相關Log
[self printRequestMessage:task];
NSLog(@"failure:???\n%@",error.localizedDescription);
}];
}
- (void)postWithURL:(NSString *)URLString parameters:(NSDictionary *)parameters {
[self POST:URLString
parameters:parameters
success:^(SPRequest *request, NSString *responseString) {
if ([self.delegate respondsToSelector:@selector(SPRequest:finished:)]) {
[self.delegate SPRequest:request finished:responseString];
}
}
failure:^(SPRequest *request, NSError *error) {
if ([self.delegate respondsToSelector:@selector(SPRequest:Error:)]) {
[self.delegate SPRequest:request Error:error.description];
}
}];
}
- (void)getWithURL:(NSString *)URLString {
[self GET:URLString parameters:nil success:^(SPRequest *request, NSString *responseString) {
if ([self.delegate respondsToSelector:@selector(SPRequest:finished:)]) {
[self.delegate SPRequest:request finished:responseString];
}
} failure:^(SPRequest *request, NSError *error) {
if ([self.delegate respondsToSelector:@selector(SPRequest:Error:)]) {
[self.delegate SPRequest:request Error:error.description];
}
}];
}
- (void)cancelAllOperations{
[self.operationQueue cancelAllOperations];
}
- (void)printRequestMessage:(NSURLSessionDataTask *)task {
/*
// 請求URL
NSLog(@"請求URL:%@\n",task.originalRequest.URL);
// 請求方式
NSLog(@"請求方式:%@\n",task.originalRequest.HTTPMethod);
// 請求頭信息
NSLog(@"請求頭信息:%@\n",task.originalRequest.allHTTPHeaderFields);
// 請求正文信息
NSLog(@"請求正文信息:%@\n",[[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:NSUTF8StringEncoding]);
*/
// 請求響應時間
NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:self.startRequestDate];
NSLog(@"請求響應時間:%@\n",@(time));
NSLog(@"\n請求URL:%@\n請求方式:%@\n請求頭信息:%@\n請求正文信息:%@\n請求響應時間:%@\n",task.originalRequest.URL,task.originalRequest.HTTPMethod,task.originalRequest.allHTTPHeaderFields,[[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:NSUTF8StringEncoding],@(time));
}
@end