123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // HTTPDataProcession.m
- // Unity-iPhone
- //
- // Created by duowan123 on 2021/2/2.
- //
- #import "HTTPDataProcession.h"
- #define RequestSerializerType_JSON @"application/json"
- #define RequestSerializerType_URLEncoded @"application/x-www-form-urlencoded"
- #define RequestSerializerType_FormData @"form-data"
- @interface HTTPDataProcession ()<NSURLSessionDataDelegate>
- @end
- @implementation HTTPDataProcession
- #pragma mark - 网络请求数据
- /// 向网络请求数据
- + (void)getHTTPDataProcession:(NSString*)urlString token:(NSString*)token success:(SuccessBlock)successBlock fail:(FailBlock)failBlock{
-
- // 1.创建url
- // 请求一个网页
- // NSString *urlString = @"http://test-shoes-api.hiyd.com/forum/friends?limit=1000";
- // 一些特殊字符编码
- urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- NSURL *url = [NSURL URLWithString:urlString];
-
- // 2.创建请求 并:设置缓存策略为每次都从网络加载 超时时间30秒
- NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
- //2.创建一个 NSMutableURLRequest 添加 header
- NSMutableURLRequest *mutableRequest = [request mutableCopy];
- [mutableRequest addValue:token forHTTPHeaderField:@"token"];
- [mutableRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- //3.把值覆给request
- request = [mutableRequest copy];
-
- // 3.采用苹果提供的共享session
- NSURLSession *sharedSession = [NSURLSession sharedSession];
- // 4.由系统直接返回一个dataTask任务
- NSURLSessionDataTask *dataTask = [sharedSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
- if (data && (error == nil)){
-
- // 网络请求完成之后就会执行,NSURLSession自动实现多线程
- successBlock(data);
-
- } else {
- // 网络访问失败
- for (NSString * key in error.userInfo){
-
- if ([key isEqualToString:@"com.alamofire.serialization.response.error.data"]){
-
- id errorObject = [NSJSONSerialization JSONObjectWithData:error.userInfo[key] options:1 error:nil];
-
- NSLog(@"错误信息 ERROR =========== >>> %@",error);
- failBlock(errorObject);
- }
-
- }
- }
-
- }];
- // 5.每一个任务默认都是挂起的,需要调用 resume 方法
- [dataTask resume];
-
- }
- /// 向网络请求数据
- + (void)uploadHTTPDataWithUrlString:(NSString *)urlString withParams:(NSDictionary *)params token:(NSString*)token success:(SuccessBlock)successBlock fail:(FailBlock)failBlock{
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
- request.HTTPMethod = @"POST";
- request.timeoutInterval = 10.0f;
-
- NSString *keyValueBody = [self getKeyValueStringWithDictionary:params];
- request.HTTPBody = [keyValueBody dataUsingEncoding:NSUTF8StringEncoding];
-
- NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
- sessionConfiguration.HTTPAdditionalHeaders = @{@"Content-Type":@"application/x-www-form-urlencoded;charset=utf-8",@"token":token};
-
- NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
-
- NSURLSessionDataTask *dataTask= [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
-
- if (data && (error == nil)){
-
- // 网络请求完成之后就会执行,NSURLSession自动实现多线程
- successBlock(data);
-
- }else{
-
- // 网络访问失败
- for (NSString * key in error.userInfo){
-
- if ([key isEqualToString:@"com.alamofire.serialization.response.error.data"]){
-
- id errorObject = [NSJSONSerialization JSONObjectWithData:error.userInfo[key] options:1 error:nil];
-
- failBlock(errorObject);
- }
-
- }
- }
-
- }];
-
- //发送请求
- [dataTask resume];
-
- }
- //
- +(NSString*)getKeyValueStringWithDictionary:(NSDictionary*)dic{
-
- if (!dic||dic.count==0) {
- return @"";
- }
-
- NSString *result = @"";
- NSArray *theAllKeys = [dic allKeys];
- for (int i=0; i<theAllKeys.count; i++) {
- NSString *theKey = [theAllKeys objectAtIndex:i];
- NSString *theValue = [dic objectForKey:theKey];
-
- if (i==0) {
- NSString *temp = [NSString stringWithFormat:@"%@=%@", theKey, theValue];
- result = [result stringByAppendingString:temp];
- }else {
- NSString *temp = [NSString stringWithFormat:@"&%@=%@", theKey, theValue];
- result = [result stringByAppendingString:temp];
- }
- }
- NSLog(@"result = %@",result);
- return result;
- }
- @end
|