2015-12-18 53 views
1

我使用的是NSURLConnect電話,我得到不支持的URL時,我有我的查詢字符串的空間。爲了解決這個問題,我將字符串轉換爲base64,並且仍然收到錯誤。如何成功通過NSUrlConnect發送base64字符串?如果這是不可能的,我該如何安全地發送一個有空格的字符串?使用Base64串NSURLConnection的

NSData *convertData = [queryWS dataUsingEncoding: NSUnicodeStringEncoding]; 
queryWS = [convertData base64EncodedStringWithOptions:kNilOptions]; 

NSURLRequest *urlRequest = 
[NSURLRequest requestWithURL:[NSURL URLWithString:queryWS]]; 
NSURLResponse *response = nil; 
NSError *error = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest 
            returningResponse:&response 
               error:&error]; 
if (!error) { 
    responseDictionary = 
    [NSJSONSerialization JSONObjectWithData:data 
            options:0 
            error:&parseError]; 

回答

0
NSString *queryString = @"SOME_QUERY_STRING"; 
NSString *encodedQueryString = [queryString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 
1

你不希望將URL字符串轉換爲Base64編碼。你想要的是正確地轉義URL中的任何特殊字符。如果您只需要支持iOS 9或更高版本,請參閱NSString stringByAddingPercentEscapesUsingEncoding:方法或NSString stringByAddingPercentEncodingWithAllowedCharacters: