没有合适的资源?快使用搜索试试~ 我知道了~
iOS NSURLSession实现网络请求
0 下载量 115 浏览量
2021-01-20
08:45:51
上传
评论
收藏 21KB PDF 举报
温馨提示
试读
1页
发送POST请求 NSURL *url = [NSURL URLWithString:@”http://127.0.0.1/demo.json”]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @”post”; NSString *body = @”username=123&password=123″; request.HTTPBody = [body dataUsingEncoding:NSUTF8String
资源详情
资源评论
资源推荐
iOS NSURLSession实现网络请求实现网络请求
发送POST请求
NSURL *url = [NSURL URLWithString:@”http://127.0.0.1/demo.json”];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @”post”;
NSString *body = @”username=123&password=123″;
request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response, NSError * _Nullable error) {
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@”%@”,json);
}] resume];
发送GET请求
NSURL *url = [NSURL URLWithString:@”http://127.0.0.1/demo.json”];
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse *
_Nullable response, NSError * _Nullable error) {
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@”%@”,json);
}] resume];
文件下载
NSURL *url = [NSURL URLWithString:@”abc.hm”];
[[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location,
NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@”%@”,[NSThread currentThread]);
NSLog(@”%@”,location);
NSString *doc = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
lastObject] stringByAppendingPathComponent:@”123.hm”];
//复制到其他位置
[[NSFileManager defaultManager] copyItemAtPath:location.path toPath:doc error:NULL];
}] resume];
作者:BianHuanShiZhe
weixin_38667581
- 粉丝: 8
- 资源: 955
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0