开发者

Trouble getting a 401 response (unauthorized) from an iPhone app

I have a synchronous method to send out requests that looks like the following:

+ (NSString *)sendRequest:(NSMutableURLRequest *)request {
    NSHTTPURLResponse *response;
    NSError *error;

    NSData *responseData = 
 [NSURLConnection sendSynchronousRequest:request                         
        returningResponse:&response 
           error:&error];

 NSLog(@"response = %@", response);
 NSLog(@"error = %@", [error localizedDescription]);

    NSString *responseString = 
 [[NSString alloc] initWithData:responseData 
        encoding:NSUTF8StringEncoding];
    [responseString autorelease];

    NSLog(@"%@: %@", [request HTTPMethod], [request URL]);
    NSLog(@"Response Code: %d", [response statusCode]);
    NSLog(@"Content-Type: %@", [[response allHeaderFields] 
                                objectForKey:@"Content-Type"]);
    NSLog(@"Response: %@\n\n", responseString);   

    return responseString;
}

For a particular request, I am getting a null response with an error saying "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)"

According to the Foundation constants reference this code is NSURLErrorUserCancelledAuthentication which is described as

Returned when an asynchronous request for authentication is cancelled by the user.

This is typically incurred by clicking a "Cancel" button in a username/password dialog, rather than the user making an attempt to authenticate.

However, I there is no user clicking cancel. I am simply making a request to retrieve a list of events without being authorized and therefore the 401 I return should produce an unauthorized response, not a null response with a NSURLErrorUserCancelledAuthentication error. My Rails code that returns this response is the following:

def require_auth
    # Return unauthorized if the API call was not made by an authorized user
    unless current_user
      respond_to do |format|
        #format.json { render :text => "Unauthorized", :status => :unauthorized }
  开发者_C百科      format.json { head :unauthorized }
        format.xml { head :unauthorized }
      end
    end   
end

and the output from my Rails log looks like this:

Started GET "/events.json" for
127.0.0.1 at Fri Jan 07 10:51:47 -0500 2011   
Processing by EventsController#index as JSON Completed 401 Unauthorized in 0ms

Does anyone know why this is happening and how to create the correct response?


I'm more of a Rails guy than an iPhone guy, but quick googling (see thread here: http://discussions.apple.com/thread.jspa?messageID=7915337) that NSURLConnection only returns a NSHTTPURLResponse when it's successful (200) and otherwise returns the null response and the behavior you're seeing on a 401.

It looks like this is the nature of the API and you'll need to use something lower level if you want to distinguish between different non-200 status responses.

This other Stackoverflow thread seems to imply the asynchronous version of the API will let you get all of the response codes and data: iOS: How can i receive HTTP 401 instead of -1012 NSURLErrorUserCancelledAuthentication

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜