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

Alhazred

macrumors member
Original poster
Jul 5, 2011
35
0
Code:
[richiesta setHTTPBody:[[NSString stringWithFormat:@"macaddress=%@&user=%@&titolo=%@&trama=%@",appDelegate.macaddress,appDelegate.nomeutente,self.titolo.text,self.trama.text] dataUsingEncoding:NSASCIIStringEncoding]];

NSLog(@"Mac: %@ \n\n Titolo: %@ \n\n Trama: %@",appDelegate.macaddress,self.titolo.text,self.trama.text);
Why the first line sends emty values to the php script, while the second prints them correctly?

I've tied to put strings in the request to the php script and they arrives, so the code for the request is correct.
I do not understand...
 
Are you trying to send a GET or POST request? HTTP bodies don't apply to GET requests, the parameters need to encoded into the URL for GET requests. If you attempting a POST request, you need set that up with setHTTPMethod:.
 
I'm using POST
Code:
NSMutableURLRequest *richiesta = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sito.it/iosphp/trame.php"]
														 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[richiesta setHTTPMethod:@"POST"];
as said if I use something like this the request works:
Code:
NSString *mac = @"2323232";
NSString *user = @"user";
NSString *tit = @"titolo";
NSString *tra = @"trama";
...
[richiesta setHTTPBody:[[NSString stringWithFormat:@"macaddress=%@&user=%@&titolo=%@&trama=%@",mac,user,tit,tra] dataUsingEncoding:NSASCIIStringEncoding]];
 
It looks that the problem is caused by the presence of some special character such as '
Those characters are present inside the database text but not shown on the iPad display and they cause the malfunction, if there are no ' in the text, the request works correctly.
I do I have to manage those characters?
 
Solved the problem with ´ that it is different from ', I've changed them inside the database.
Now the problem are the characters like è à ù ì ò é

Using NSUTF8StringEncoding the request is sent to the script and it reads the parameters, but those characters are read not correctly.

How to correctly manage those characters?
 
I see, but shouldn't that be done by dataUsingEncoding:NSUTF8StringEncoding used for the NSMutableURLRequest object?

There are also other characters which need to be percent-encoded, such as ^ or | and they are correctly sent doing nothing.
 
I see, but shouldn't that be done by dataUsingEncoding:NSUTF8StringEncoding used for the NSMutableURLRequest object?

How would the NSMutableURLRequest know what to encode and what to leave alone?

You're passing it a sequence of bytes that looks something like this:
Code:
macaddress=somethingHere&user=NameHere&titolo=etc&trama=1234
It's not going to percent-encode that, because it doesn't know which parts are literal (the &'s and ='s) and which parts aren't.

You need to percent-encode your parameter values BEFORE you use them with stringWithFormat:. There is an NSString method for percent encoding. Look it up.

Imagine what will happen if your titolo and trama values are @"k=35%" and @"B&W". The expanded result would be:
Code:
macaddress=somethingHere&user=NameHere&titolo=k=35%&trama=B&W

Get a tool like WireShark and look at the data in the POST request. See if it's what you expect.
http://www.wireshark.org/about.html


There are also other characters which need to be percent-encoded, such as ^ or | and they are correctly sent doing nothing.
Just because those happen to work doesn't mean it's correct. All it means is that your POST body didn't happen to use those for a reserved purpose.
 
Before to pass the strings to setHTTPBody
I've parsed them with
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

same result.
 
Before to pass the strings to setHTTPBody
I've parsed them with
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

same result.

Post your actual code. We can't debug isolated fragments or descriptions. We need to see the exact code you're using.

Consider the possibility that I may want to compile it and test it here.


Apply basic testing and debugging skills.

Consider making a small test program that uses specific test cases that you know will work. Then make a small change to it that introduces a problematic character, such as single-quote or one of your accented letters. Retest.

In short, make a program that you know works; test it; then make the smallest change that causes it to fail.
 
This is the code
Code:
- (void)editTramaDone:(id)sender {
	[trama resignFirstResponder];
	
	TabularRecInfoAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	
	NSString *parsedUser = [appDelegate.nomeutente stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSString *parsedTitolo = [self.titolo.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSString *parsedTrama = [self.trama.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	NSMutableURLRequest *richiesta = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sito.it/iosphp/trame.php"]
															 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	[richiesta setHTTPMethod:@"POST"];
	[richiesta setHTTPBody:[[NSString stringWithFormat:@"user=%@&mac=%@&titolo=%@&trama=%@", parsedUser, appDelegate.macaddress, parsedTitolo, parsedTrama] dataUsingEncoding:NSUTF8StringEncoding]];

	NSURLResponse *response;
	NSError *error;
	NSData *dati = [NSURLConnection sendSynchronousRequest:richiesta returningResponse:&response error:&error];
	
	NSString *esitoModifica = [[[NSString alloc] initWithData:dati encoding:NSASCIIStringEncoding] autorelease];
	NSLog(@"\n%@",esitoModifica);
	self.navigationItem.rightBarButtonItem = nil;
}
 
What have you done to debug this? Be specific.

Have you NSLog'ed intermediate data, such as the individual percent-escaped strings? If so, post the actual NSLog output.

Exactly what is the value of appDelegate.macaddress, which is not being percent-escaped? Post its actual NSLog output.

Have you broken out the data before it's set as HTTP body and NSLog'ed that? Post the actual NSLog output.

For example, I would want to see the formatted string here, BEFORE it's converted to NSData:
Code:
setHTTPBody:[[NSString stringWithFormat:@"user=%@&mac=%@&titolo=%@&trama=%@", parsedUser, appDelegate.macaddress, parsedTitolo, parsedTrama] dataUsingEncoding:NSUTF8StringEncoding]

Have you dumped the bytes of the NSData as hex and confirmed that they are all valid, and the structure is well-formed (no extra bytes, no null bytes)? What does an NSLog of the NSData produce? Post the output.


Have you used the debugger to set breakpoints, and examined the data at each point?


You've described nothing about the HTTP response.

What output is produced by NSLog'ing the NSURLResponse's significant properties?

What output is produced by NSLog'ing the NSData?

What output is produced by NSLog'ing the NSData converted to string?


Effective debugging requires you to break things down into smaller pieces, and confirm that the smaller pieces are correct. You've barely begun to really break this down into smaller pieces, and you haven't posted any NSLog output.

We can't see your screen. All we know about this is what you tell us.
 
Problem solved, in the code I sent the request using UTF8 while I was reading the response with ASCII encoding, so I thought that also the script where reading the parameters in a wrong way xD

Thanks for your help. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.