0

I am sending HTTP post request with NSURLConnection synchronous request as soon as the user signs in to a server. The cookies are used for authentication and am not setting any cookie as header.

case 1: I receive a success response when I launch the application for the first time and sign in.

case 2: Later, when I quit the application with signed in state and later relaunch the application, sign out and sign in again, i receive -1012 error.

case 3: The error is not occurring on logging out in the same session as I logged in. For instance, if I sign in on app launch and sign out, then quit the app, again relaunch and sign in, I receive a success response.

Please help me with second case.

My post request is as follows:

// perform eHTTPPost request with header

- (NSData *)doPostRequest:(NSURL *)serverURL
                 data:(NSData *)postData
    headerContentType:(NSString *)contentType
{
   NSData *returnData;
   _connectionException = nil;
   NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)    [postData length]];

   HTTPClient *client = [[HTTPClient alloc] initWithRequestType:eHTTPPOST
                                             withServerURL:[serverURL absoluteString]
                                                   andPath:nil
                                            andQueryString:nil
                                                   andData:postData];


  [client setRequestDataContentType: contentType];
  [client setRequestDataContentLength:postLength];
  [client setShouldZipEncode:NO];
  if([client sendRequest]){
     returnData = [client responseData];
     NSError *parseError = nil;
     NSString *responseDataString = [NSString stringWithUTF8String:[returnData bytes]];
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:returnData
                                                             options:NSJSONReadingAllowFragments
                                                               error:&parseError];
   NSLog(@"Set attributes Response Dict: %@",responseDict);
  }else{
//Handle error
    _connectionException = [client responseException];
    NSLog(@"HTTP error : %@", [client responseException].reason);
  }
  return returnData;
}

1 Answer 1

0

Either the cookies are expiring or they aren't getting persisted across launches. Although you could try to fix the cookie persistence problem, relying on an auth cookie to still be around is not the ideal way to handle authentication.

Instead, your app should store the user's credentials in the keychain, and should sign in to retrieve an auth cookie upon launch, asking the user to provide credentials if the sign-in fails or if no credentials exist in the keychain.

If you do this, then you can handle the above case trivially by just signing in again with the stored credentials whenever a request fails with -1012 (HTTP 401), and if it fails, ask the user for new credentials.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.