我必須發送一個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