iOS: How can i receive HTTP 401 instead of -1012 NSURLErrorUserCancelledAuthentication

后端 未结 5 1114
一整个雨季
一整个雨季 2020-12-01 01:57

I have a problem similar to the one described in the link below.

NSHTTPURLResponse statusCode is returning zero when it should be 401

I use [NSURLConne

5条回答
  •  悲&欢浪女
    2020-12-01 02:55

    I'm surprised at the "recommended" answer to this thread.

    Fine, I'm sure using the async version methods are better, but it still doesn't explain why the sendSynchronousRequest function does let you pass in a variable to return the response code, but in some circumstances, it just returns nil.

    It' 4 years since this issue was reported, I'm using XCode 6.2 with iOS 8.2, and this ancient bug is still present.

    My web services deliberately return a 401 error when a user's username & password aren't correct.

    When my iPhone app calls this service (with the wrong credentials)...

    NSHTTPURLResponse *response = nil;
    NSData *data = [ NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error ];
    

    ..response is returned as nil (so I can't test for the HTTP Response 401), error receives a -1012 message wrapped up like this:

        Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" 
        UserInfo=0x174869940 {NSErrorFailingURLStringKey=https://mywebservice.com/Service1.svc/getGroupInfo/6079, NSUnderlyingError=0x174e46030 
        "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)",
    NSErrorFailingURLKey=https://mywebservice.com/Service1.svc/getGroupInfo/6079}
    

    and the sendSynchronousRequest function returns a lengthy XML string containing... well... this...

    
    
    
      
        Request Error
        
      
      
        

    Request Error

    The server encountered an error processing the request. The exception message is 'Access is denied.'. See server logs for more details. The exception stack trace is:

    at System.ServiceModel.Dispatcher.AuthorizationBehavior.Authorize(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

    Come on Apple, fix your bugs...

提交回复
热议问题