我试图将加密的json字符串发布到服务器,但无论我如何使用AFNetworking,服务器都不接受发布数据。
我与Postman合作:
我尝试使用的AFNetworking代码:
var encrypted: String = "70RFFwboFukuO068d2trNRgv............"
let manager = AFHTTPRequestOperationManager(baseURL: NSURL(string: baseUrl))
manager.requestSerializer = AFHTTPRequestSerializer()
manager.requestSerializer.setValue("application/aesjson-server; charset=utf-8", forHTTPHeaderField: "Content-Type")
manager.requestSerializer.setValue("gzip_aes", forHTTPHeaderField: "Accept-Encoding")
manager.POST(postUrl, parameters: encrypted, success:
{ (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("Success")
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Failed \(error.description)")
})
加密字符串是base64编码的,据我所知,服务器在到达时解码时遇到问题。
修改
感谢Zaph的评论,看起来好像AFNetworking正在向帖子数据的开头添加(null)=,并且url编码base64字符串。
发布数据:
L9u7AvNdtJFLA7VaNR1115idL2h4tjnJ/Ij9Me+4m7svmi6x7E8RBVvNqEcNFx9YEP5595ntvfl5/05YvfztOVpBD08Ftk6AI59Sbskss+/bvW9Tnwi5h1d9e07DVSqkm6TaTl/jwnrTv/HCgI4eTIqJXcwlTz6TqUUkLvgTPj1fZKB1Fus9s4OleJVR+gX+
变为:
(null)=L9u7AvNdtJFLA7VaNR1115idL2h4tjnJ%2FIj9Me%2B4m7svmi6x7E8RBVvNqEcNFx9YEP5595ntvfl5%2F05YvfztOVpBD08Ftk6AI59Sbskss%2B%2FbvW9Tnwi5h1d9e07DVSqkm6TaTl%2FjwnrTv%2FHCgI4eTIqJXcwlTz6TqUUkLvgTPj1fZKB1Fus9s4OleJVR%2BgX%2B
有没有办法阻止AFNetworking这样做?
答案 0 :(得分:0)
这是绝对合乎逻辑的。
Post Data应该是一个NSDictionary,其中指定了键/值对。 您正在发布字符串而不是字典,因此密钥设置为nil。
let postData:NSDictionary = ["postKey": encrypted];
...
manager.POST(postUrl, parameters: postData, success:
{ (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("Success")
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Failed \(error.description)")
})
答案 1 :(得分:0)
这适用于我的具体情况。我需要覆盖查询字符串序列化块,因此AFNetworking没有添加密钥,并且url对发布数据进行编码:
manager.requestSerializer.setQueryStringSerializationWithBlock { (request: NSURLRequest!, dict: [NSObject : AnyObject]!, error: NSErrorPointer) -> String! in
var postString = ""
for (key, object) in dict {
postString += object as String
}
return postString
}