NSURLSessionDataTask发送Get/Post请求

<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>

你可能感兴趣的:(NSURLSessionDataTask发送Get/Post请求)