开发者

Sending data with POST, issue with character encoding

I am having issues with my NSURLRequest and doing a POST whenever the string includes %, for example.

On my website, I can see that the encoding is ISO-8859-1,utf-8 for Accept-Charset but for some reason I cannot get the POST to work if it includes "%&$" etc. It seems like it won't encode it with UTF-8 encoding. Am I doing something wrong in the below code?

NSData *postData = [credentials dataUsingEncoding:NSUTF8StringEncoding allow开发者_运维百科LossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.website.com/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


You may want to hear "Content-Transfer-Encoding".

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: binary

or if you have problems specially with those chars

Content-Transfer-Encoding: base64

EDIT: The "application/x-www-form-urlencoded" makes the "%" signs happen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜