您的位置:首页 > 理论基础 > 计算机网络

网络demo

2016-06-06 07:34 465 查看
普通网络请求// Do any additional setup after loading the view, typically from a nib.
AFHTTPRequestOperationManager *man = [AFHTTPRequestOperationManager manager];
//data string
//data json xml
//man 下载回来的数据类型 默认 json
//修改返回数据类型
//data AFHTTPResponseSerializer
//json AFJSONResponseSerializer
//XML AFXMLParserResponseSerializer
man.responseSerializer = [AFHTTPResponseSerializer serializer];
//url username
//responseObject 返回数据
AFHTTPRequestOperation *op = [man GET:@"http://map.onegreen.net/%E4%B8%AD%E5%9B%BD%E6%94%BF%E5%8C%BA2500.jpg" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[responseObject writeToFile:@"/Users/lcy/Desktop/test.png" atomically:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"123");
}];

//本次 bytesRead
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"%lu %lld %lld",bytesRead,totalBytesRead,totalBytesExpectedToRead);
}];

上传

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSLog(@"%@",info);
//获取点击图片
self.imageView.image = info[UIImagePickerControllerOriginalImage];

AFHTTPRequestOperationManager *man = [AFHTTPRequestOperationManager manager];
man.responseSerializer = [AFHTTPResponseSerializer serializer];

[man POST:@"http://10.0.8.8/sns/my/upload_headimage.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//name 服务器设定的
[formData appendPartWithFileData:UIImagePNGRepresentation(info[UIImagePickerControllerOriginalImage]) name:@"headimage" fileName:@"test.png" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil][@"message"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

[picker dismissViewControllerAnimated:YES completion:^{

}];
}


原生请求

#import "ViewController.h"

//登录 ---> url?username=zhangsan&pass=123456
//数据安全

//数据大小 1024 1MB 10Mb
//http GET POST 请求头 请求体 PUT(增) Delete
//http ----> url

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//get
#if 0
NSURL *url = [NSURL URLWithString:@"http://api.douban.com/v2/movie/top250?start=0&count=20&apikey=02d830457f4a8f6d088890d07ddfae47"];

NSURLRequest *requset = [NSURLRequest requestWithURL:url];

NSData *data = [NSURLConnection sendSynchronousRequest:requset returningResponse:nil error:nil];

NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
#endif

//post
NSURL *postURl = [NSURL URLWithString:@"http://api.douban.com/v2/movie/top250"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:postURl];
//设置请求方式 post
[req setHTTPMethod:@"POST"];

NSString *arg = @"start=20&count=20&apikey=02d830457f4a8f6d088890d07ddfae47";
//设置请求体的内容
[req setHTTPBody:[arg dataUsingEncoding:NSUTF8StringEncoding]];

//设置请求头
[req setValue:@"sadfasdfaf" forHTTPHeaderField:@"asfa"];

NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];

NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: