美文网首页
url encode decode

url encode decode

作者: Rui哥 | 来源:发表于2023-03-19 15:53 被阅读0次

url 参数局部encode

/** url 局部参数加密
  比如: http://baidu.cou?abc=中国&add=北京&link=http://xinna.com?userName=张三
  中参数link对应的部分 http://xinna.com?userName=张三 就需要encode
  1.  encode 后的数据为: http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
 
  2. 不encode数据与 encode 数据拼接  http://baidu.cou?abc=中国&add=北京&link=http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
 */
NSString * encodeUrlQueryValue(NSString *string) {
    NSString * charaters = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "; //要编码的字符
    NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:charaters] invertedSet];
    return [string stringByAddingPercentEncodingWithAllowedCharacters:set];
}

url 参数encode 应用


-(void)demo{
 // 想要传输的数据
    NSString *test = @"http://baidu.cou?abc=中国&add=北京&a_link=http://xinna.com?userName=张三";
    NSLog(@"test: %@",test);
    
    // 1. 取出子链接
    NSString *link = @"http://xinna.com?userName=张三";
    NSLog(@"a_link: %@",link);
    // 2. encode 子链接
    NSString *encodedLink = encodeUrlQueryValue(link);
    NSLog(@"encodedLink: %@",encodedLink);
    
    // 3. 拼接子链接
//    NSString *urlStr = [NSString stringWithFormat:@"http://baidu.com/abc/bcd.htm?abc=中国&add=北京&a_link=%@", encodedLink];
    NSString *urlStr = [NSString stringWithFormat:@"native:///baidu.com/abc/bcd.htm?abc=中国&add=北京&a_link=%@", encodedLink];
    NSLog(@"urlStr+encodedLink: %@",urlStr);
    
    // 4. 编码整个链接
    NSString *output =[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"output: %@",output);
     
    NSString *rawLink = [encodedLink stringByRemovingPercentEncoding];
    NSLog(@"rawUrlStr: %@",rawLink);
    
    // 5. 创建url组件
    NSURLComponents *comp =  [[NSURLComponents alloc] initWithString:output];
    // 6. 获取组件路径
    NSLog(@"comp.path: %@", comp.path);
    // 7. 获取组件查询
    NSLog(@"comp.query: %@", comp.query);
    
    for(NSURLQueryItem *item in comp.queryItems){
        NSLog(@"name: %@, value: %@", item.name, item.value);
        if ([item.name hasPrefix:@"a_"]) {
             
            NSLog(@"value1: %@", [item.value stringByRemovingPercentEncoding]);
            NSLog(@"value2: %@", [item.value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
        }
    }
     
    // native:///circle?path=search&page=1&i_homePage=zzz&a_homePage=zzz&c_homePage=zzz
    
    // 这个可以还原整个url 编码
    NSString *rawUrlStr =[urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"rawUrlStr: %@",rawUrlStr);

}

输出结果:

dm[11720:309751] test: http://baidu.cou?abc=中国&add=北京&a_link=http://xinna.com?userName=张三
dm[11720:309751] a_link: http://xinna.com?userName=张三
dm[11720:309751] encodedLink: http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
dm[11720:309751] urlStr+encodedLink: native:///baidu.com/abc/bcd.htm?abc=中国&add=北京&a_link=http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
dm[11720:309751] output: native:///baidu.com/abc/bcd.htm?abc=%E4%B8%AD%E5%9B%BD&add=%E5%8C%97%E4%BA%AC&a_link=http%253A%252F%252Fxinna.com%253FuserName%253D%25E5%25BC%25A0%25E4%25B8%2589
dm[11720:309751] rawUrlStr: http://xinna.com?userName=张三
2023-03-20 15:51:58.765678+0800 dm[11720:309751] comp.path: /baidu.com/abc/bcd.htm
dm[11720:309751] comp.query: abc=中国&add=北京&a_link=http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
dm[11720:309751] name: abc, value: 中国
dm[11720:309751] name: add, value: 北京
dm[11720:309751] name: a_link, value: http%3A%2F%2Fxinna.com%3FuserName%3D%E5%BC%A0%E4%B8%89
dm[11720:309751] value1: http://xinna.com?userName=张三
dm[11720:309751] value2: http://xinna.com?userName=张三
dm[11720:309751] rawUrlStr: native:///baidu.com/abc/bcd.htm?abc=中国&add=北京&a_link=http://xinna.com?userName=张三

相关文章

网友评论

      本文标题:url encode decode

      本文链接:https://www.haomeiwen.com/subject/ojoykdtx.html