2016-10-13 56 views
0

我想要一个例子来理解如何使用目标C中的NSURLSession进行“GET”请求以及如何获得响应?NSURLSession请求和响应

+0

有几十这个问题 –

+0

https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started –

+0

爵士提供的答案,我使用目标C 。请建议一些教程,解释如何在目标c中使用NSURLSession从服务器获取“GET”请求并获取响应。 – FreshStar

回答

2

GET

NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"give your url here"]]; 

//create the Method "GET" 
[urlRequest setHTTPMethod:@"GET"]; 

NSURLSession *session = [NSURLSession sharedSession]; 

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
{ 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
    if(httpResponse.statusCode == 200) 
    { 
    NSError *parseError = nil; 
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; 
    NSLog(@"The response is - %@",responseDictionary); 
    } 
    else 
    { 
    NSLog(@"Error");  
    } 
}]; 
[dataTask resume];