URL 编码问题

URL 编码问题

一般来说,我们通过使用 NSString 的stringByAddingPercentEscapesUsingEncoding函数来将字符串转换成可用的 URL,如下面这个例子:

NSString *string = @"this is a string";
NSURL *url = [NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",url);

输入结果为

this%20is%20a%20string

但,当我们传入一些URL 的保留字符(例如&=+$)时,结果变成这样  
输入

NSString *string = @"this is a &=+$ string";

输出

this%20is%20a%20&=+$%20string

保留字符并没有没编码,这会导致我们将这个 URL 进行传输使用时,产生一些未知错误, 那么如何解决这个问题呢,苹果文档也在这个函数的说明中提供了相关资料.

Adds all percent escapes necessary to convert the receiver into a legal URL string. 
Uses the given encoding to determine the correct percent escapes (returning nil if the given encoding cannot encode a particular character).
See CFURLCreateStringByAddingPercentEscapes in CFURL.h for more complex transformations

下面提供CFURLCreateStringByAddingPercentEscapes的用法,

- (NSString *) encodeURLFromString:(NSString *) string {
    NSString *result;
    CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                  (CFStringRef)string,
                                                                  NULL,
                                                                  (CFStringRef)@";/?:@&=+$,",
                                                                  kCFStringEncodingUTF8);
    result = (NSString *)CFBridgingRelease(encoded);
    return result;
}

- (NSString *) decodeURLToString:(NSString *) url {
    NSString *result;
    CFStringRef decoded = CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault,
                                                                     (CFStringRef)url,
                                                                     CFSTR(""));
    result = (NSString *)CFBridgingRelease(decoded);
    return result;
}

这样,编码时,我们调用CFURLCreateStringByAddingPercentEscapes函数,进行解析时使用CFURLCreateStringByReplacingPercentEscapes还原即可,仍使用上面这个例子

NSString *string = @"this is a &=+$ string";
NSURL *url = [NSURL URLWithString:[self encodeURLFromString:string]];
NSLog(@"%@",url);

输出结果变为

this%20is%20a%20%26%3D%2B%24%20string

这样,我们就解决了 URL 编码过程中的问题.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplate在发送HTTP请求时,有时会出现URL编码问题。根据引用\[1\]和引用\[3\]的内容,可以看出在发送请求前,可以通过设置RestTemplate的消息转换器来解决URL编码问题。具体做法是使用`restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8))`来设置编码为UTF-8。这样可以确保URL中的参数不会被错误地编码为%25。另外,根据引用\[2\]的内容,还可以通过自己封装URI的方式来解决URL编码问题,具体做法是使用`URI.create(url + myparams + signUrl)`来创建URI对象,然后使用`restTemplate.postForEntity(uriObj, request, String.class)`来发送请求。这样可以确保URL中的参数不会被错误地编码。 #### 引用[.reference_title] - *1* [SpringBoot RestTemplate 解决编码问题 UTF8](https://blog.csdn.net/weixin_42195284/article/details/114935420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springboot中使用restemplate,请求接口会将url参数的%编码为%25问题解决](https://blog.csdn.net/weixin_45973130/article/details/120896847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值