• 网络请求方法(SDK封装可以替换afn)


    //个人觉得 sdk开发时候 最好不要用第三方 最好可以用最原始的方法 替换 此处仅做sdk封装使用  留存
    +(void)GET:(NSString *)urlStr params:(NSDictionary *)params sucess:(void (^)(NSURLSessionDataTask *task, id responseObject))sucess failure:(void (^)(NSURLSessionDataTask *task, NSError * error))failure { urlStr = [self URLEncodedStringValue:params url:urlStr]; NSURL *URL = [NSURL URLWithString:urlStr]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; [request setHTTPMethod:@"GET"]; NSURLSession *session = [NSURLSession sharedSession]; __block NSURLSessionDataTask *task= nil; task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { if (error) { failure(task,error); } else { NSError *jsonError = nil; id notesJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError]; if (!jsonError) { if (sucess) { sucess(task,notesJSON); } } else { if (failure) { failure(task,jsonError); } } } }]; [task resume]; } + (NSString *)URLEncodedStringValue:(NSDictionary *)Parameter url:(NSString *)urlStr { if (Parameter) { urlStr = [urlStr stringByAppendingString:@"?"]; NSArray *keys =[Parameter allKeys]; for (int i=0;i<keys.count;i++) { NSString *key = [keys objectAtIndex:i]; NSString *value = [Parameter objectForKey:key]; if (i==0) { urlStr = [urlStr stringByAppendingFormat:@"%@=%@",key,value]; } else { urlStr = [urlStr stringByAppendingFormat:@"&%@=%@",key,value]; } } } urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet]; return urlStr; }
  • 相关阅读:
    3个检测浏览器UserAgent信息的网站
    linux系统下配置网桥(CentOS 5.5)
    squid封禁不带UserAgent的请求
    iptables透明网桥无法使用透明代理错误
    Squid修改用户浏览器的Useragent信息
    MySQL主从复制(MasterSlave)与读写分离(MySQLProxy)实践
    js中格式化时间
    js获取当前月的第一天和最后一天
    JS String.toDate
    Js获取当前日期时间及其它操作
  • 原文地址:https://www.cnblogs.com/dujiahong/p/6674074.html
Copyright © 2020-2023  润新知