使用AFNetworking进行get和post请求
作者:互联网
网络请求框架
pod 'AFNetworking', '~> 4.0'
-
TestAFNetworking.h代码
#import <Foundation/Foundation.h> #import "AFHTTPSessionManager.h" #import "TestDelegateManager.h" NS_ASSUME_NONNULL_BEGIN @interface TestAFNetworking : NSObject +(void)get:(NSString*)name:(NSString*)pass; +(void)post:(NSString*)name:(NSString*)pass; +(void)postJson:(NSString*)name:(NSString*)pass; @end NS_ASSUME_NONNULL_END
-
TestAFNetworking.m代码
#import "TestAFNetworking.h" @implementation TestAFNetworking // 获取当前时间戳 +(NSString*)timestamp{ NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval time=[date timeIntervalSince1970]*1000; return [NSString stringWithFormat:@"%.0f", time]; } #pragma mark - get请求示例 +(void)get:(NSString*)name:(NSString*)pass{ // 请求url NSString* url = @"http://192.168.1.161:80/test/test_get_"; // 请求query参数 NSDictionary *parameters = @{ @"name":name, @"pass":pass }; // 请求header参数[设置方式一] NSDictionary *headers = @{ @"timestamp":[self timestamp] }; // 设置请求超时* NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration]; conf.timeoutIntervalForRequest = 30.0f; // 创建请求管理器* AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager]initWithSessionConfiguration:conf]; // 使用系统默认最大并发* manager.operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount; // 格式化请求出入参* manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // 请求header参数[设置方式二(若在格式化请求出入参前添加则后台接收不到)]* // ... [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"platform"]; // 响应头content type设置* NSSet *sets = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", @"application/atom+xml",@"application/xml", @"text/xml",@"image/png",@"image/jpeg", @"charset=utf-8", nil]; manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:sets]; // 具体请求 [manager GET:url parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSString *json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"请求成功url:%@,json:%@", task.currentRequest.URL, json); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败url:%@,error:%@", url, error); }]; } #pragma mark - post请求示例 +(void)post:(NSString*)name:(NSString*)pass{ NSString *url = @"http://192.168.1.161:80/test/test_post_"; NSDictionary *parameters = @{ @"name":name, @"pass":pass }; NSDictionary *headers = @{ @"timestamp":[self timestamp] }; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; /*manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer];*/ NSSet *sets = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", @"application/atom+xml",@"application/xml", @"text/xml",@"image/png",@"image/jpeg", @"charset=utf-8", nil]; manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:sets]; [manager POST:url parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"请求成功url:%@,name:%@,pass:%@", task.currentRequest.URL, responseObject[@"name"], responseObject[@"pass"]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败url:%@,error:%@", url, error); }]; } #pragma mark - post发送json请求示例 +(void)postJson:(NSString*)name:(NSString*)pass{ NSString *url = @"http://192.168.1.161:80/test/test_json_"; NSDictionary *parameters = @{ @"name":name, @"pass":pass }; NSDictionary *headers = @{ @"timestamp":[self timestamp] }; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; NSSet *sets = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain", @"application/atom+xml",@"application/xml", @"text/xml",@"image/png",@"image/jpeg", @"charset=utf-8", nil]; manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:sets]; [manager POST:url parameters:parameters headers:headers progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"请求成功url:%@,name:%@,pass:%@", task.currentRequest.URL, responseObject[@"name"], responseObject[@"pass"]); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"请求失败url:%@,error:%@", url, error); }]; } @end
-
调用示例
- (IBAction)testGet:(id)sender { [TestAFNetworking get:@"catface":@"password"]; } - (IBAction)testPostJson:(id)sender { [TestAFNetworking postJson:@"catface":@"password"]; } - (IBAction)testPost:(id)sender { [TestAFNetworking post:@"catface" :@"password"]; }
标签:name,get,url,text,pass,manager,NSString,AFNetworking,post 来源: https://blog.csdn.net/itCatface/article/details/115008211