您的位置:首页 > 其它

NSURLSessionDataTask发送Get/Post请求

2016-04-17 13:22 309 查看
<span style="font-size:14px;">//
//  ViewController.m
//  NSURLSession
//
//  Created by hq on 16/4/17.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];
}

-(void) post{

NSURLSession *session=[NSURLSession sharedSession];

NSString *urlString=@"http://xxxx/login";

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

request.HTTPMethod=@"POST";

request.HTTPBody=[@"username=aa&pwd=aa" dataUsingEncoding:NSUTF8StringEncoding];

NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

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

}];

[task resume];

}

-(void) dataTaskWithURL{

//获取session对象
NSURLSession *session=[NSURLSession sharedSession];

NSString *urlString=@"http://120.25.226.186:32812/login?username=aa&pwd=aa";

NSURLSessionDataTask *task=[session dataTaskWithURL:[NSURL URLWithString: urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

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

}];

[task resume];
}

@end
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: