我必须发送一个https GET请求到我在Swift 1.2中开发的iPhone应用程序中的Web服务。在URL中编码“&”问题
我想构建查询字符串参数,但得到编码之前发送到服务器。
当密码包含'&'字符时,所有好但不工作。预计将'&'字符编码为'%26',但不能正常工作...
刚做了'%'测试。按预期工作,'%'提供'%25'。但不能转换 '&' 的标志....
尝试以下方法:
var testPassword1: String = "mypassword&1"
var testPassword2: String = "mypassword%1"
// Try to encode 'testPassword1'
testPassword1.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
testPassword1.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
// Try to encode 'testPassword2'
testPassword2.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
testPassword2.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
我已经做了上述测试,以下是响应
想知道这样做的正确方法。谢谢。
除了这个问题,我绝不会在GET请求中发送明文密码。 – vadian
看看http://stackoverflow.com/a/31791424/1187415。 –
你也应该考虑使用NSURLComponents&NSURLQueryItem。 – jtbandes