这是我试图调用的网址:
https://myapp.something.com/IAMSMART.TFS/tfsdefects?username=austinSan\mcs234&password=Houston456
在我的代码中,我这样调用它:
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"https://myapp.something.com/IAMSMART.TFS/tfsdefects?username=austinSan\\mcs234&password=Houston456"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}]resume];
出于某种原因,我总是将jsonObject视为nil。原始URL和我的URL之间的唯一区别是由于转义字符而添加了额外的“\”。
答案 0 :(得分:0)
你需要百分之百逃避你的GET参数
austinSan\mcs234
实际应该是
austinSan%5Cmcs234
AFNetworking使用以下方法(line 66)执行此操作:
static NSString * AFPercentEscapedQueryStringKeyFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
static NSString * const kAFCharactersToLeaveUnescapedInQueryStringPairKey = @"[].";
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescapedInQueryStringPairKey, (__bridge CFStringRef)kAFCharactersToBeEscapedInQueryString, CFStringConvertNSStringEncodingToEncoding(encoding));
}
完成