Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daisyhedson

macrumors newbie
Original poster
Aug 21, 2009
5
0
I'm using the following code to find the response status.

Code:
NSHTTPURLResponse* authenticationResponse = nil;	
	NSMutableURLRequest *authenticationRequest = [NSMutableURLRequest requestWithURL:url];
	[authenticationRequest setHTTPMethod:@"POST"];
	[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
   
	NSData *responseData = [NSURLConnection sendSynchronousRequest:authenticationRequest returningResponse:&authenticationResponse error:&error]; 
NSLog(@"Response Code: %d",[authenticationResponse statusCode]);
NSLog(@"Response string: %@",responseStr);

In my server side coding(Java Servlet), I set the response status using the following code:

Code:
if ( data == null )
            {
                logger.log(info, "Username and password do not match for user:{0}", uName);
                response.setStatus(2001);
                out.write("Username and Password does not match"); //No I18N
            }
            else
            {
                response.setStatus(200);
                out.write(data);
            }

My console displays,

Code:
Response Code: 200
Response string: Username and Password does not match

Here is my problem:
Though I set response to 2001 at server side coding, I used to receive response status as 200 always(I'm getting the right response string).
Help me to receive response status as 2001 when the data is null.

Thanks in advance for help.
 
Well HTTP response codes are 3 digits long and 2001 is not really a valid HTTP response code - so what you are seeing is undefined behavior.

The standard response code when authentication fails is 401 (HTTP Unauthorized).
 
Thanks for the quick reply. I changed my server side code as,

Code:
if ( data == null )
            {
                logger.log(info, "Username and password do not match for user:{0}", uName);
                response.setStatus(401);
                out.write("Username and Password does not match"); //No I18N
            }
            else
            {
                response.setStatus(200);
                out.write(data);
            }


Now my console displays,

Code:
Response Code: 0
Response string: Username and Password does not match

Now I'm receiving response code as 0 instead of 401. Is there any way to get response code as 401? Am I missing something? Kindly help.
 
You need to use the sendError method instead of setStatus.
setStatus() is for successful response only.

Also use SC_UNAUTHORIZED instead of 401 - depending on implementation of the servlet code using 401 or 200 or whatever may not work. Always use the documented codes.

[ PS I know no one wants to read the documentation - but believe me it helps a lot :) ]
 
Thanks for your help and Sorry for my ignorance. I changed my server side coding as per your instructions. It is working fine.:)
Please go through my client side coding and help me to get the status 401.

Code:
NSHTTPURLResponse* authenticationResponse = nil;	
	NSMutableURLRequest *authenticationRequest = [NSMutableURLRequest requestWithURL:url];
	[authenticationRequest setHTTPMethod:@"POST"];
	[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
   
	NSData *responseData = [NSURLConnection sendSynchronousRequest:authenticationRequest returningResponse:&authenticationResponse error:&error]; 
NSLog(@"Response Code: %d",[authenticationResponse statusCode]);
NSLog(@"Response string: %@",responseStr);
NSLog(@"Error: %@",error);

Here is my output:

Code:
Response Code: 0

Response string: <html><head><title>Apache Tomcat/5.0.28 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>This request requires HTTP authentication ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.0.28</h3></body></html>


Error: Error Domain=NSURLErrorDomain Code=-1012 UserInfo=0x4c81e0 "Operation could not be completed. (NSURLErrorDomain error -1012.)"
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.