标签:
https://github.com/AFNetworking/AFNetworking
下面也有具体的使用方法
下载好解压,把AFNetworking文件夹拖入项目
引入头文件
#import "AFNetworking.h"
//创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//设置服务器端返回的数据格式,默认为JSON解析AFJSONResponseSerializer AFXMLResponseSerializer AFResponseSerializer
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
设置get请求后面的参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"123";
//设置url
NSString *string = @"http://localhost:8080/TFServer/login";
[mgr GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//获取成功以后执行这个代码块
NSLog(@"---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败执行的代码
NSLog(@"error");
}];
//创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//设置服务器端返回的数据格式,默认为JSON解析AFJSONResponseSerializer AFXMLResponseSerializer AFResponseSerializer
mgr.responseSerializer = [AFJSONResponseSerializer serializer];
设置get请求后面的参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"123";
//设置url
NSString *string = @"http://localhost:8080/TFServer/login";
//只是这里把get变为POST
[mgr POST:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//获取成功以后执行这个代码块
NSLog(@"---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败执行的代码
NSLog(@"error");
}];
新式的方法,使用与iOS7
//得到文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"1.jpg" ofType:nil];
//创建一个request对像
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8080/TFServer/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"test.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
//创建一个manager对象
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
//创建任务
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
将上传的文件转换为data数据然后上传
// 1.创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封装参数(这个字典只能放非文件参数)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"age"] = @20;
params[@"pwd"] = @"456";
params[@"height"] = @1.55;
// 2.发送一个请求
NSString *url = @"http://localhost:8080/TFServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
UIImage *image = [UIImage imageNamed:@"1.jpg"];
NSData *fileData = UIImageJPEGRepresentation(image, 1.0);
[formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
// 不是用这个方法来设置文件参数
// [formData appendPartWithFormData:fileData name:@"file"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
直接用URL获取要上传的文件
// 1.创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封装参数(这个字典只能放非文件参数)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"age"] = @20;
params[@"pwd"] = @"456";
params[@"height"] = @1.55;
// 2.发送一个请求
NSString *url = @"http://localhost:8080/TFServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在发送请求之前会自动调用这个block
// 需要在这个block中添加文件参数到formData中
/**
FileURL : 需要上传的文件的URL路径
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
*/
NSURL *url = [[NSBundle mainBundle] URLForResource:@"1" withExtension:@"jpg"];
NSLog(@"-----%@", url);
[formData appendPartWithFileURL:url name:@"file" fileName:@"test.jpg" mimeType:@"image/jpeg" error:nil];
/**
FileData : 需要上传的文件的具体数据
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
*/
// UIImage *image = [UIImage imageNamed:@"minion_01"];
// NSData *fileData = UIImagePNGRepresentation(image);
// [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
标签:
原文地址:http://blog.csdn.net/ttf1993/article/details/46494603