2016-10-13 34 views
0

目的CのNSURLSessionを使用して「GET」要求がどのように行われ、どのように応答が得られるのかを理解した例が必要ですか?NSURLSession要求と応答

+0

をGETこの質問 –

+0

の利用できる回答の数十がありますhttps://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started –

+0

卿、私は客観的Cを使用しています"GET"リクエストがどのように行われ、レスポンスがNSURLSessionを使用してサーバから取得されたかを説明するチュートリアルを提案してください。 – FreshStar

答えて

2

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