【问题标题】:Sending data with POST, issue with character encoding使用 POST 发送数据,字符编码问题
【发布时间】:2010-01-26 20:13:40
【问题描述】:

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

在我的网站上,我可以看到ISO-8859-1,utf-8 的编码是ISO-8859-1,utf-8,但由于某种原因,如果它包含“%&$”等,我无法让 POST 工作。似乎它不会编码它使用 UTF-8 编码。我在下面的代码中做错了吗?

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

【问题讨论】:

    标签: iphone httprequest nsmutableurlrequest


    【解决方案1】:

    您可能想听“内容传输编码”。

    内容类型:文本/纯文本; charset=UTF-8

    内容传输编码:二进制

    或者如果你对这些字符有特别的问题

    内容传输编码:base64

    编辑: “application/x-www-form-urlencoded”使“%”符号发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      相关资源
      最近更新 更多