ios - 将&符号字符转换为utf 8

标签 ios swift encoding

我将带有数据的 post 请求发送到我的服务器:

var syncData = "data=Hello&World"
let urlRequest = NSMutableURLRequest(URL: NSURL(string: url)!, cachePolicy:  NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: NSTimeInterval(60))

urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = syncData.dataUsingEncoding(NSUTF8StringEncoding)

但是服务器无法解码&(ampersand) 字符的问题。在服务器上我只得到“你好”这个词。这不是服务器问题。因为我试图用 java 和 python 和 curl 发送这个字符串。
一切正常。

我试过这个: NSXMLParser problem with "&"(Ampersand) character

但这没有帮助...有什么想法吗?

最佳答案

正如穆萨所说。解决方案将是:

let customAllowedSet =  NSCharacterSet(charactersInString:"=\"#%/<>?@\\^`{|}&").invertedSet
    syncData = syncData.stringByAddingPercentEncodingWithAllowedCharacters(customAllowedSet)!

    urlRequest.HTTPBody = syncData.dataUsingEncoding(NSUTF8StringEncoding)

关于ios - 将&符号字符转换为utf 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519078/

相关文章:

iOS 在后台线程上访问 UIScreen

ios - SKStoreReviewController 在用户第一次启动应用程序时显示

ios - 无法使 HTTP 基本身份验证工作

ruby-on-rails - 在 GET 请求中传递句号(句点)和正斜杠?

ios - iOS 中的方形视频输出

ios - 使用swift访问解析中的类

ios - UITextView如何实现搜索功能?

ios - 带有 SpriteKit 的 UITableView

c - 使用霍夫曼编码的文本数据压缩

iphone - iOS 中 ID3 标签的正确编码