Reputation: 1227
Trying to postdata in post connection using setHttpPost
NSString* strRequest = [NSString stringWithFormat:@"username=%@&password==%@&client_assertion_type=%@&client_assertion=%@&spEntityID=%@",
strUsrName,strPasswrd,@"JWT",strAppTknContent,@"http://localhost:8080/opensso"];
NSData* dataRequest = [NSData dataWithBytes:[strRequest UTF8String] length:[strRequest length]];
[theRequest setHTTPMethod: @"POST"];
[theRequest setHTTPBody: dataRequest];
want to encode the value for username,password,client_assertion_type alone utf-8 type
NSString* username = [ encode utf-8 type @"fff"];
how do i encode only the value of each key elements and post it
Upvotes: 0
Views: 2043
Reputation: 78905
You cannot encode a string in UTF-8 and then put it in a NSString
instance. NSString
uses Unicode characters anyway but not in an encoded form.
Why would you want to encode them separately anyway? What exactly are you trying to achieve?
And just to point out a mistake in your code. The following line does not work once you have characters that require more than 1 byte in UTF-8:
NSData* dataRequest = [NSData dataWithBytes:[strRequest UTF8String] length:[strRequest length]];
The length
parameter is expected to be the number of bytes but you are passing the number of characters.
A correct and simpler alternative is:
NSData* dataRequest = [strRequest dataUsingEncoding: NSUTF8StringEncoding ];
Update:
As you obviously want to properly URL encoded your parameters, you're probably looking for code like this:
NSString* strRequest = [NSString stringWithFormat:@"username=%@&password==%@&client_assertion_type=%@&client_assertion=%@&spEntityID=%@",
[strUsrName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[strPasswrd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
@"JWT",
[strAppTknContent stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[@"http://localhost:8080/opensso" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
];
NSData* dataRequest = [strRequest dataUsingEncoding: NSUTF8StringEncoding ];
Update 2:
It's a very strange design to transmit data that way. When it was first used, it probably was a hack to get something working quickly. Unfortunately, it became a standard.
Basically, you have two levels of encoding. The URL encoding to serves to put several key/value pairs into a single string such that the string can later be split again into the key/value pairs. It needs to know that a UTF-8 encoding is used on the second level so it can escape the problematic characters. It's does UTF-8 the string yet.
The second level translates the string (a sequence of characters) into a byte stream using the UTF-8 encoding since HTTP is specified as a transmission of bytes, not characters.
Upvotes: 1