// 請求數據類實例化
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
// 可變request實例化
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:str]];
// 設置 請求方法我POST
request.HTTPMethod = @"POST";
// 設置請求頭 的 Content-Type格式
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString *postStr = [NSString stringWithFormat:@"content=%@",mdic];
[request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
// 請求數據
NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger responseStatusCode = [httpResponse statusCode];
// NSLog(@"---------%@ %ld %@", httpResponse, (long)responseStatusCode ,responseObject);
if (responseStatusCode == 200) {
// 成功后的處理
// NSLog(@"%@", responseObject);
// NSLog(@"返回數據為!!!%@" , responseObject);
successResponse(responseObject);
}else {
// 失敗后的處理
// NSLog(@"%@", error);
if(error.code==-1009){
NSDictionary *codeDic=@{@"errCode":@"-1009",@"msg":@"網絡未連接!"};
successResponse(codeDic);
}else{
NSDictionary *codeDic=@{@"errCode":APPERROR,@"msg":@"未知錯誤!"};
successResponse(codeDic);
}
}
}];
[dataTask resume];