Tuesday 4 April 2017

URL Calling with cookies

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    [sessionConfiguration setHTTPAdditionalHeaders:@{@"Content-type": @"application/json"}];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
    
    NSString *url;
    

         url=[NSString stringWithFormat:@"%@"];

     NSLog(@"%@",url);
    
    NSURL *jsonURL = [NSURL URLWithString:url];
    
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:jsonURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    // Convert your data and set your request's HTTPBody property
    
    
    
    
    NSString *session_id=[[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    
  
    
    NSLog(@"sessionid:%@",session_id);
    
    
 // cookie and session id assigning 
    
    NSString *cookieValue =@"347784784378478";
    
    NSString *cookieName = @"sessionid";
    
    
    NSLog(@"%@",[NSString stringWithFormat:@"%@=%@", cookieName, cookieValue] );
    
   
    [request setValue:[NSString stringWithFormat:@"%@=%@", cookieName, cookieValue] forHTTPHeaderField:@"Cookie"];
    
    
    [request setHTTPMethod:@"GET"];
    

    
    
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    
    
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                  {
                                      
                                      
                                      
                                      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
//                                      NSLog(@"DEMO2:%@",error);
                                      if (error.code==-1003 || error.code==-1009 || error.code==-1017)
                                      {
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              
                                              dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                                                  //[self doSomeWork];
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      [ self.hud hideAnimated:YES];
                                                      //
                                                      [self message:error.localizedDescription];
                                                  });
                                              });
                                              
                                          });
                                      }
                                      
                                      if (!error)
                                      {
                                          
                                          
                                          NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
                                          
                                         
//                                           NSLog(@"jsonData1:%@",jsonData);
//                                          
//                                           NSLog(@"jsonData1:%ld",(long)httpResponse.statusCode);
                                          
                                          //|| httpResponse.statusCode == 201
                                          if (httpResponse.statusCode == 417 || httpResponse.statusCode == 405)
                                          {
                                              
                                              dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                                                  //[self doSomeWork];
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      
                                                      [ self.hud hideAnimated:YES];
                                                      
                                                      [self message:[jsonData valueForKey:@"error"]];
                                                  });
                                              });

                                          }
                                          
                                          
                                          if (httpResponse.statusCode == 200 || httpResponse.statusCode == 201)
                                          {
                                              NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
                                              
                                              
//                                              NSLog(@"jsonData3:%@",jsonData);
                                              
                                              NSString *responseCode = [NSString stringWithFormat:@"%@",[jsonData valueForKey:@"response_code"]];
                                              
                                              if ([responseCode isEqualToString:@"3"])
                                              {
                                                  dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                                                      //[self doSomeWork];
                                                      dispatch_async(dispatch_get_main_queue(), ^{
                                                          
                                                          [ self.hud hideAnimated:YES];
                                                          
                                                        
                                                      });
                                                  });
                                                  
                                              }
                                              else
                                              {
                                                  if ([responseCode isEqualToString:@"0"])
                                                  {
                                                      
                                                      dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                                                          //[self doSomeWork];
                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                              [ self.hud hideAnimated:YES];
                                                             
                                                              
                                                              
                                                        
                                                              
                                                          });
                                                      });
                                                      
                                                      
                                                      
                                                  }
                                              }
                                              
                                              
                                              
                                              
                                          }
                                          
                                          else
                                          {
                                              NSLog(@"Error:%@",error);
                                              if (error.code==-1003 || error.code==-1009 ||error.code==-1001)
                                              {
                                                  dispatch_async(dispatch_get_main_queue(), ^{
                                                      
                                                      [self message:error.localizedDescription];
                                                  });
                                                  
                                              }
                                              
                                              NSLog(@"Error:%@",error);
                                              //            [self.HUD removeFromSuperview];
                                          }
                                      }
                                      
                                      
                                  }];
    [task resume];

    

No comments:

Post a Comment