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

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
Hi all..

In my app, i have a string say "Tony & Guy", i want to send it to the server through NSConnection....

I convert above string to NSData,and assigning it as HTTPBody...

But my server is throwing error as it parse "&" (the above string is a value for a node in an xml file)...

In java version, i converted the "&" to "%26" and POSTed it , working successfully..

Here also i tried with "%26" and "&"...Failed both times..

So on converting XMLstring to XMLData (NSData) using [myString -dataUsingStringEncoding:], which encoding i want to use???

I tried with NSUTF8Encoding and NSASCIIEncoding...

Still failed..

So my qstn is
tony %26 guy = [tony & guy stringUsingEncoding:????
Help....
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
It isn't an encoding issue. It is a URL escaping issue.

String encoding talks about the byte representation of the string (ASCII, UTF8, etc). Personally, I would do all network transfers of strings in UTF8 as it is the most compatible.

URL escaping is making sure that text can be made to not clash with the control characters used in URLs, and is independent of the string encoding.

You will probably want to write a small method that takes an NSString, and escapes it. I am not sure AppKit/Cocoa provides one for you.

Now, if we got a bit more information as to the problem (specifics), then we should be able to give a more specific answer.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
^^ You can use NSString's stringByReplacingPercentEscapesUsingEncoding: to remove spaces, and NSURL's standardizedURL may help.
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
It isn't an encoding issue. It is a URL escaping issue.

String encoding talks about the byte representation of the string (ASCII, UTF8, etc). Personally, I would do all network transfers of strings in UTF8 as it is the most compatible.

URL escaping is making sure that text can be made to not clash with the control characters used in URLs, and is independent of the string encoding.

You will probably want to write a small method that takes an NSString, and escapes it. I am not sure AppKit/Cocoa provides one for you.

Now, if we got a bit more information as to the problem (specifics), then we should be able to give a more specific answer.


I worte a method which converts to convert symbols and spcl chars to escape codes, But Still it failing....

I am using UTF8 encoding only...

If i am pasting the same url in browser its working....

means i want to use some other method of NSURL or NSString for successfull URL creation...

What i am doing is ...

create a MutableRequest, init with a URL...

add an following string in NSData form as HTTPBody...

Code:
myChoice=
<category>
<sub> tony & guy</sub>
<sub> item 2 </sub>
<category>

I worte a method to convert it to..
Code:
myChoice=
<category>
<sub> tony %26 guy</sub>
<sub> item 2 </sub>
<category>

In both cases my app fail...
But in browser the second xml is a valid "POST"...where first is not..
But in my Iphone app, both are invalid...
 

sujithkrishnan

macrumors 6502
Original poster
May 9, 2008
265
0
Bangalore
worked fine...

hi frnz..

everything worked fine with ....


stringByAddingPercentEscapesUsingEncoding ....

and my method to convert '&' to "%26"....

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