使用 [NSURL URLWithString:urlString]
方法生成URL时,由于urlString
不符合 URL
规范导致生成 URL
失败的问题:
可以使用下面的方法先对 urlString
字符串 进行校验(两种方法的区别有待探究)
方法一
使用stringByAddingPercentEncodingWithAllowedCharacters:
NSString *urlStr = [[NSString stringWithFormat:@"%@/%@?device=%@&sysver=%@&%@&%ld", CONFIG_URL, CONFIG_URL_KEYWORD, device, sysver, readHistoryStr, (long)TIMESTEMP] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
方法二
使用stringByAddingPercentEscapesUsingEncoding:
NSString *urlStr = [[NSString stringWithFormat:@"%@/%@?device=%@&sysver=%@&%@&%ld", CONFIG_URL, CONFIG_URL_KEYWORD, device, sysver, readHistoryStr, (long)TIMESTEMP] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];