使用POST发送数据,出现字符编码问题 [英] Sending data with POST, issue with character encoding

查看:54
本文介绍了使用POST发送数据,出现字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的 NSURLRequest 遇到了问题,并且每当字符串包含%时都会执行POST.

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

在我的网站上,我可以看到 Accept-Charset 的编码为 ISO-8859-1,utf-8 ,但是由于某种原因,我无法获得POST如果它包含%& $"等,则可以正常工作.似乎它不会使用UTF-8编码对其进行编码.我在下面的代码中做错了吗?

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 allowLossyConversion: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];

推荐答案

您可能想听听"Content-Transfer-Encoding".

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

Content-Type:文本/纯文本;charset = UTF-8

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

Content-Transfer-Encoding:二进制

Content-Transfer-Encoding: binary

或者如果您对这些字符有特殊的疑问

or if you have problems specially with those chars

Content-Transfer-Encoding:base64

Content-Transfer-Encoding: base64

"application/x-www-form-urlencoded"使%"符号出现.

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

这篇关于使用POST发送数据,出现字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆