NSMutableUrlRequest 吃加号

发布于 2024-08-25 21:27:25 字数 454 浏览 4 评论 0原文

我创建 NSMutableUrlRequest 用于将数据发送到服务器,向其中添加所有必需的字段,然后添加用于发送的字符串,如下所示:

[theRequest setHTTPBody:[postString dataUsingEncoding: NSUTF8StringEncoding]];

postString 是一个常见的 NSString。

问题是,当我在服务器上收到此请求时,所有加号 (+) 都会从 http 正文中消失。因此,如果我在 iPhone 上有“abcde+fghj”,我会在服务器上得到“abcde fghj””。

这可能是使用 dataUsingEncoding: NSUTF8StringEncoding 造成的一些编码问题吗?或者是某些 NSMutableUrlRequest 剥离功能?我该怎么做才能让它停止剥离加号?我需要在服务器端接收 UTF8 字符串。

I create NSMutableUrlRequest for sending data to server, add all necessary fields to it and then add the string for sending like this:

[theRequest setHTTPBody:[postString dataUsingEncoding: NSUTF8StringEncoding]];

postString is a usual NSString.

The problem is, when I receive this request at the server, all the plus (+) signs disappear from the http body. So if I had "abcde+fghj" on iPhone, I get "abcde fghj" on the server".

Can this be some encoding problem from using dataUsingEncoding: NSUTF8StringEncoding? Or some NSMutableUrlRequest stripping feature? What can I do to make it stop stripping plus signs? I need to receive UTF8 strings at the server side.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や三分注定 2024-09-01 21:27:25

加号 (+) 是空间的标准快捷方式,位于URL 的查询字符串部分。如果您想要一个文字 +,请将其编码为 %2b。

The plus (+) sign is a standard shortcut for a space, in a URL's query string portion. If you want a literal +, encode it as %2b.

太阳哥哥 2024-09-01 21:27:25

可能是服务器不知道 POST 正文的编码是什么。
您是否尝试将 charset=UTF-8 添加到请求的标头中,如下所示:

[theRequest setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

May be the server doesn't know which is the encoding of the POST body.
Have you tried to add the charset=UTF-8 into the header of your request like that:

[theRequest setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
秋叶绚丽 2024-09-01 21:27:25

例如,您需要使用 "Content-Type" -> 将 data 发布到后端“应用程序/x-www-form-urlencoded”

application/x-www-form-urlencoded:键和值编码为
键值元组以“&”分隔,键和值之间有“=”
价值。键和值中的非字母数字字符均为百分比
编码:这就是为什么这种类型不适合与
二进制数据(使用 multipart/form-data 代替)

参考

您可以通过将函数 addingPercentEncoding 应用于 Swift 中的字符串来对数据进行百分比编码:

guard let jsonString = String(data: jsonData, encoding: .utf8)?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
   failureCompletion()
   return
}

var request = URLRequest(url: url)
...
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = "jsonString".data(using: .utf8)
...

For e.g. you need to post data to the backend with a "Content-Type" -> "application/x-www-form-urlencoded"

application/x-www-form-urlencoded: the keys and values are encoded in
key-value tuples separated by '&', with a '=' between the key and the
value. Non-alphanumeric characters in both keys and values are percent
encoded: this is the reason why this type is not suitable to use with
binary data (use multipart/form-data instead)

Reference

You can percent-encode your data by applying function addingPercentEncoding to your string in Swift:

guard let jsonString = String(data: jsonData, encoding: .utf8)?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
   failureCompletion()
   return
}

var request = URLRequest(url: url)
...
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = "jsonString".data(using: .utf8)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文