网络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.
}
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.
}
相关文章推荐
- httpd之使用rewrite对目录进行访问控制
- Android官方开发文档Training系列课程中文版:连接无线设备之网络服务搜索功能
- Volley网络请求框架
- http协议详解
- TCP 的那些事儿系列
- OkHttp3浅析
- 知识付费还是内容付费?网络时代的内容、体验与注意力
- Ceph心跳与网络
- IDC行业往何处去?
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- Linux 网络 I/O 模型简介(图文)
- IDC行业往何处去?
- https对接相关
- No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC
- 5.5 神经网络的正则化
- 网络七层及模型&&网络数据包
- 5.4 Hessian矩阵
- Java 网络编程之无网不欢
- iOS开发网络篇—NSURLConnection基本使用
- IP地址、子网掩码、网络号、主机号、网络地址、主机地址(转)