| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- //
- // HttpClient.m
- // zhuxun
- //
- // Created by 张建伟 on 15/4/7.
- // Copyright (c) 2015年 张建伟. All rights reserved.
- //
- #import "HttpClient.h"
- #import "NSObject+Common.h"
- //导入安全相关库
- #import "MD5.h"
- #import "DES.h"
- #import "AFNetworkReachabilityManager.h"
- #import "Utils.h"
- #import "AFNetworking.h"
- @implementation HttpClient
- + (HttpClient *)sharedJsonClient {
- HttpClient *httpclient = [[HttpClient alloc]init];
-
- return httpclient;
- }
- - (void)requestJsonDataWithPath:(NSString *)aPath
- withParams:(id)params
- withMethodType:(int)NetworkMethod
- andBlock:(void (^)(id data, NSError *error))block{
- //log请求数据
- NSLog(@"Params: %@",params);
- #warning 安全改造--加密请求 完成前不要取消下面注释
-
- AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
- mgr.requestSerializer = [AFJSONRequestSerializer serializer];
- mgr.securityPolicy.allowInvalidCertificates = YES;
- mgr.requestSerializer.timeoutInterval = 10;
-
- mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", @"text/html",nil];
- [mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-
-
- [mgr.requestSerializer setValue:[NSString stringWithFormat:@"Basic %@",[DES returnKeyValue]]forHTTPHeaderField:@"Authorization"];
- NSLog(@"aPath: - / - / - aPath: %@ aPath: - / - / -",aPath);
-
- //新增无网络不处理请求,直接return
- // [Utils netWorkType:^AFNetworkReachabilityStatus status) {
-
-
- //发起请求
- switch (NetworkMethod) {
- case Get:{
-
- [mgr GET:aPath parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
- //
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- //
- if ([task.originalRequest.URL.path isEqualToString:@"/v1/servers"]) {
-
- block(responseObject, nil);
- return ;
-
- }
-
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- //
- block(nil, error);
-
- [self showError:error];
- }];
-
- // [self GET:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
- // id error = [self handleResponse:responseObject];
- // if (error) {
- // block(nil, error);
- // }else{
- // block(responseObject, nil);
- // }
- // } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, error);
- // [self showError:error];
- // block(nil, error);
- // }];
- break;
- }
- case Post:{
- //通过服务器获取数据
- [mgr POST:aPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- id error = [self handleResponse:responseObject];
- if (error) {
-
-
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
-
- }];
- // [self POST:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
- //
- //
- //
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
- //
- //
- // NSLog(@"\n===========responseMessage===========\n%@",responseObject[@"message"]);
- //
- // id error = [self handleResponse:responseObject];
- // if (error) {
- //
- //
- // block(nil, error);
- // }else{
- // // if ([aPath isEqualToString:kNetPath_Code_Login]) {
- // // [CodingNetAPIClient saveCookieData];
- // // }
- // block(responseObject, nil);
- // }
- // } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, error);
- // [self showError:error];
- // block(nil, error);
- // }];
- break;
- }
- case Put:{
-
- [mgr PUT:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
- }];
-
- // [self PUT:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
- // id error = [self handleResponse:responseObject];
- // if (error) {
- // block(nil, error);
- // }else{
- // block(responseObject, nil);
- // }
- // } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, error);
- // [self showError:error];
- // block(nil, error);
- // }];
- break;
- }
- case Delete:{
-
- [mgr DELETE:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- [self showError:error];
-
- block(nil, error);
- }];
-
- // [self DELETE:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
- // id error = [self handleResponse:responseObject];
- // if (error) {
- // block(nil, error);
- // }else{
- // block(responseObject, nil);
- // }
- // } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- // NSLog(@"\n===========response===========\n%@:\n%@", aPath, error);
- // [self showError:error];
- //
- // block(nil, error);
- // }];
- }
- default:
- break;
- }
-
- // } fail:^{
- // //
- //
- // return ;
- // }];
-
- // AFNetworkReachabilityStatus status = [userInfo[AFNetworkingReachabilityNotificationStatusItem] integerValue];
-
- // if (status == AFNetworkReachabilityStatusNotReachable) {//未连接
- // //这是在手机的顶部出现的提示
- // [weakself showStatusBarNetError:[NSError errorWithDomain:@"NETWOR" code:-1 userInfo:@{@"NSLocalizedDescription":@"您的手机目前没有连接网络,请进行网络确认"}]];
- // // [self socketDisConnect];
- // [weakself disConnect];
- // }else if(status==AFNetworkReachabilityStatusReachableViaWWAN||status==AFNetworkReachabilityStatusReachableViaWiFi)
- // {
- // //连接到主机
- // // [self connect:_ipAddress port:_port];
- // [weakself startConnect];
- // //状态栏显示连接状态
- // [weakself showStatusBarNetReconnected];
- //
- // }
-
-
- }
- - (void)cloudStorageRequestJsonDataWithPath:(NSString *)aPath
- withParams:(id)params
- withMethodType:(int)NetworkMethod
- andBlock:(void (^)(id data, NSError *error))block
- {
- //log请求数据
-
- NSLog(@"Params: %@",params);
- #warning 安全改造--加密请求 完成前不要取消下面注释
-
- AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
- mgr.requestSerializer = [AFJSONRequestSerializer serializer];
- mgr.securityPolicy.allowInvalidCertificates = YES;
-
- mgr.requestSerializer.timeoutInterval = 10;
-
- mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", @"text/html",nil];
- [mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-
- NSString *auth = [userDefaults objectForKey:@"authorization"];
-
- if (auth) {
- //[DES cloudStorageReturnKeyValue]
- [mgr.requestSerializer setValue:[NSString stringWithFormat:@"Basic %@",auth]forHTTPHeaderField:@"Authorization"];
-
- // [mgr.requestSerializer setValue:[DES cloudStorageReturnKeyValue]forHTTPHeaderField:@"Authorization"];
- }
-
-
- NSLog(@"aPath: - / - / - aPath: %@ aPath: - / - / -",aPath);
-
- //新增无网络不处理请求,直接return
- // [Utils netWorkType:^AFNetworkReachabilityStatus status) {
-
-
- //发起请求
- switch (NetworkMethod) {
- case Get:{
-
- [mgr GET:aPath parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
- //
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- //
- if ([task.originalRequest.URL.path isEqualToString:@"/v1/servers"]) {
-
- block(responseObject, nil);
- return ;
-
- }
-
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- //
- block(nil, error);
-
- [self showError:error];
- }];
- break;
- }
- case Post:{
- //通过服务器获取数据
- [mgr POST:aPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- id error = [self handleResponse:responseObject];
- if (error) {
-
-
- block(nil, error);
- }else{
-
- block(responseObject, nil);
- }
-
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
-
- }];
- break;
- }
- case Put:{
-
- [mgr PUT:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
- }];
-
- break;
- }
- case Delete:{
-
- [mgr DELETE:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- [self showError:error];
-
- block(nil, error);
- }];
- }
- default:
- break;
- }
-
-
-
-
- }
- - (void)cloudStorageDownUpJsonDataWithPath:(NSString *)aPath
- withParams:(id)params
- withMethodType:(int)NetworkMethod
- andBlock:(void (^)(id data, NSError *error))block
- {
-
- //log请求数据
-
- NSLog(@"Params: %@",params);
- #warning 安全改造--加密请求 完成前不要取消下面注释
-
- AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
- mgr.requestSerializer = [AFJSONRequestSerializer serializer];
- mgr.securityPolicy.allowInvalidCertificates = YES;
-
- mgr.requestSerializer.timeoutInterval = 10;
-
- mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", @"text/html",nil];
- [mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
-
- // [mgr.requestSerializer setValue:[DES cloudStorageDownUpReturnKeyValue] forHTTPHeaderField:@"Authorization"];
-
- [mgr.requestSerializer setAuthorizationHeaderFieldWithUsername:@"4f860649600a47508379639dba82327d" password:@"f3b58f6b935b4acd9d9e795864ad10d4"];
-
-
- NSLog(@"aPath: - / - / - aPath: %@ aPath: - / - / -",aPath);
-
- //新增无网络不处理请求,直接return
- // [Utils netWorkType:^AFNetworkReachabilityStatus status) {
-
-
- //发起请求
- switch (NetworkMethod) {
- case Get:{
-
- [mgr GET:aPath parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
- //
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- //
-
- block(responseObject, nil);
-
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- //
- block(nil, error);
-
- [self showError:error];
- }];
-
- break;
- }
- case Post:{
- //通过服务器获取数据
- [mgr POST:aPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
-
- } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- block(responseObject, nil);
-
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
-
- }];
-
- break;
- }
- case Put:{
-
- [mgr PUT:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
-
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
-
- [self showError:error];
- block(nil, error);
- }];
-
-
- break;
- }
- case Delete:{
-
- [mgr DELETE:aPath parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id error = [self handleResponse:responseObject];
- if (error) {
- block(nil, error);
- }else{
- block(responseObject, nil);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- [self showError:error];
-
- block(nil, error);
- }];
-
- }
- default:
- break;
- }
-
-
-
-
- }
- //请求用户状态
- //-(void)postRequestToUrl:(NSString *)url netWorkMethod:(int)networkMethod aPath:(NSString *)path WithParams:(id)params complete:(void (^)(BOOL, NSDictionary *))complete{
- //
- // //拼接最全路径
- // NSString *aPath = [NSString stringWithFormat:@"%@%@",APP_HOST,path];
- //
- //
- //
- // if (networkMethod == Post) {
- //
- // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:aPath]];
- //
- // //设计请求头
- // [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
- // request.HTTPMethod = @"POST";
- //
- // NSData *data = [NSJSONSerialization dataWithJSONObject:params options:(NSJSONWritingPrettyPrinted) error:nil];
- // //设置发送请求参数
- // request.HTTPBody = data;
- //
- // //发送请求
- // [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
- // if (data!=nil) {
- // NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
- //
- // if (complete && !connectionError) {
- // complete(true,dic);
- // }else{
- // complete?complete(true,dic):nil;
- // }
- // }else{
- // NSLog(@"请求错误!");
- // }
- //
- //
- // }];
- // }else{
- //
- // NSLog(@"Requested data method is wrong, 'networkMethod' except 'Post' no other way!! ");
- //
- // }
- //
- //
- //
- //
- //
- //
- //}
- @end
|