ASIHTTPRequest only works when I hardcode the strings

I have a problem that really makes me scratch my head. There seems to be no logical reason why this is happening. If I hardcode the string in NSURL, it works. But if I pass in NSString, this will not work. I confirmed that NSString is identical to the NSLog string chain, both of them.

Code that works. The server returns a 200 response code.

//NSString * urlStr = [NSString stringWithFormat:@"http://www.server.com/%@",monograph.uri]; NSURL *url = [NSURL URLWithString:@"http://www.server.com/api/monograph/com/3894.json"]; ASIHTTPRequest *request; request = [ASIHTTPRequest requestWithURL:url]; [request setUsername:appDelegate.key]; [request setPassword:appDelegate.secret]; [request setDelegate:self]; [request startAsynchronous]; 

Failed to execute code. The server returns 204 response codes for this:

 NSString * urlStr = [NSString stringWithFormat:@"http://www.server.com/%@",monograph.uri]; NSURL *url = [NSURL URLWithString:urlStr]; ASIHTTPRequest *request; request = [ASIHTTPRequest requestWithURL:url]; [request setUsername:appDelegate.key]; [request setPassword:appDelegate.secret]; [request setDelegate:self]; [request startAsynchronous]; 

When I NSLog urlStr contains:

 http://www.server.com/api/monograph/com/3894.json 

What am I missing? There must be something to explain this. -

Regards, Code

EDIT

checked the length of both lines, and they have the same lengths, so there seems to be no white spaces or anything hidden there.

+4
source share
2 answers

Cutting all new lines / spaces from a line is one of the possible reasons. And sometimes the magazine does not show you new lines.

to try

NSString * s = [monograph.uri stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

NSString * urlStr = [NSString stringWithFormat: @ "http://www.server.com/%@", s];

+1
source

Have you checked if the NSURL object that you get from the string variable is non-zero? This can help you return home in the event of a problem.

Alternatively, you can try to escape the string variable, just run something funny that goes into the stringWithFormat call.

+1
source

All Articles