美文网首页
URL字符转码

URL字符转码

作者: 天空的羁绊 | 来源:发表于2017-11-12 15:13 被阅读97次

1:使用stringByAddingPercentEscapesUsingEncoding:

1.例子:

aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

2.方法

-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc;

3.描述

1.Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
2.It may be difficult to use this function to "clean up" unescaped or partially escaped URL strings where sequences are unpredictable.
3.See CFURLCreateStringByAddingPercentEscapes for more information.

翻译:
使用给定的编码返回一个接收者的代表,以确定将接收者转换成到合法的URL字符串所需的百分比转义。
使用此函数可能难以来清理那些序列不可预测的未转义或部分转义的URL字符串。
有关详细信息,请参阅CFURLCreateStringByAddingPercentEscapes

2:使用stringByAddingPercentEncodingWithAllowedCharacters:

1.例子:

    NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    NSString *encodedUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
    NSLog(@"\n%@\n%@",encodedUrl,encodedString1);

或:
NSString *urlStr2 = [@"http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet  URLQueryAllowedCharacterSet]];

invertedSet 作用:
A character set containing only characters that don’t exist in the receiver.
Using the inverse of an immutable character set is much more efficient than inverting a mutable character set.
中文:
仅包含接收器中不存在的字符的字符集。
反转不可变字符集比反转可变字符集更有效率。

URLQueryAllowedCharacterSet 作用:
Returns the character set for characters allowed in a query URL component.
The query component of a URL is the component immediately following a question mark (?).
For example, in the URL http://www.example.com/index.php?key1=value1#jumpLink, the query component is key1=value1.
中文:
返回一个查询URL组件中允许字符的字符集。
URL的查询组件是一个紧跟着问号(?)的组件。
例如,在URL http://www.example.com/index.php?key1=value1#jumpLink中,查询组件是key1 = value1。

2.方法:

- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:
(NSCharacterSet *)allowedCharacters

3.描述:

1.Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters.
2.UTF-8 encoding is used to determine the correct percent encoded characters.
3.Entire URL strings cannot be percent-encoded.
4.This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string.
5.Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.

翻译:
返回一个由接收者制成的新字符串,使用百分比编码字符替换所有不在allowenCharacters集合中的字符
UTF-8编码用于确定正确的百分比编码字符。
整个URL字符串不能进行百分比编码。
此方法用于URL组件或子组件字符串的百分比编码,而不是整个URL字符串。
任何在allowedCharacters中的字符,在7-bit ASCII 范围之外的,都将被忽略。

3.使用CFURLCreateStringByAddingPercentEscapes

    NSString *url = @"ertehtt""p://xxdsdscrg?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
    CFStringRef encodedCFString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                          (__bridge CFStringRef) url,
                                                                          nil,
                                                                          CFSTR("?!@#$^&%*+,:;='\"`<>()[]{}/\\| "),
                                                                         kCFStringEncodingUTF8);
    NSString *encodedString1 = [[NSString alloc] initWithString:(__bridge_transfer NSString*) encodedCFString];

相关文章

  • URL字符转码

    1:使用stringByAddingPercentEscapesUsingEncoding: 1.例子: 2.方法...

  • 代码中发送http请求, java.lang.IllegalAr

    产生原因 url中有汉字或特殊字符(非字母和数字的字符),没有转码。 解决办法 将带有特殊字符或汉字的参数进行转码...

  • 网络缓存分析

    特殊字符的转译 针对于特殊字符,urlOne只能将中文进行转码,如果需要将其他特殊字符也进行转码,就必须要用url...

  • springmvc 基本使用

    web.xml 字符转码设置DispathcherServlet配置spring xml设置对应的url spri...

  • 一些用到的网址

    在线转码 用于链接中有中文字符导致的问题 url转码 - 在线编码转换 json格式化工具 在线json格式化工具...

  • JavaScript提供的URL 的编码

    1.encodeURI() encodeURI()方法用于转码整个 URL。它的参数是一个字符串,代表整个 URL...

  • AFNetworking之AFURLRequestSeriali

    AFURLRequestSerialization主要是对请求进行编码. 字符转码当发送的网络请求URL中包含了特...

  • iOS16图片不加载问题

    项目中对url进行了拼接,然后使用上述方法对url转码,导致url为空建议,仅对需要转码的部分转码ps:使用七牛云...

  • URL 中文转码

    比如:http://google.com/中国/直接转 URL 会失败 这时候需要对字符串进行转码处理

  • URL转码

    对于URL转码问题的理解:URL转码分为编码和解码两个过程。 URL编码的理解: 首先:我们需要想想URL作为一个...

网友评论

      本文标题:URL字符转码

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