IOS URL中含有特殊字符請求失敗處理


摘要:一個含有特殊字符的URL,請求失敗處理,最好需要NsLog 看請求地址對編碼的處理

//    三種處理網址的方法
      NSString *str1 = [@"http://118.212.144.158:8081/?code=123#/work" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *str2 = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)@"http://118.212.144.158:8081/?code=123#/work", (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8));

    NSString *str3 = [@"http://118.212.144.158:8081/?code=123#/work" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [self.webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
    
    NSLog(@"str1=======:%@",str1);
    NSLog(@"str2=======:%@",str2);
    NSLog(@"str3=======:%@",str3);
    
    NSLog(@"str1Request=======:%@",[NSURLRequest requestWithURL:[NSURL URLWithString:str1]]);
    NSLog(@"str2Request=======:%@",[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]);
    NSLog(@"str3Request=======:%@",[NSURLRequest requestWithURL:[NSURL URLWithString:str3]]);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM