NSURLconnection-Post

来源:互联网 发布:兴趣部落推广软件 编辑:程序博客网 时间:2024/06/05 02:13

-(void)viewDidLoad{
//1.创建请求路径url
NSURL* url = [NSURL URLWithString:@”https://comic.idreamcastle.com/api/register/?access_token=ecpzcknd9wv92hwoafgbj0r28w24h9xl“];
//2.创建请求对象
//想要发送POST,必须创建一个可变请求对象(因为请求方式默认为GET),改变请求方式实质是改变的请求对象的内容
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
//a.设置请求方式
//如果传入“GET”,就是设置成GET方式
request.HTTPMethod = @”POST”;
//b.设置请求体
//usernmae、password
NSString* body = @”username=18010674021&password=123456”;

request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];//3.发送请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {    //        NSLog(@"%@",response);    //        NSLog(@"%@",data);    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];    NSLog(@"%@",dic[@"errfor"][@"username"]);}];

}

0 0
原创粉丝点击