使用编码加(%2B)创建NSURL [英] Make an NSURL with an encoded plus (%2B)

查看:158
本文介绍了使用编码加(%2B)创建NSURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在GET请求中传递带时区偏移的时间戳,例如

I need to pass a timestamp with a timezone offset in a GET request, e.g.,


2009-05-04T11:22: 00 + 01:00

2009-05-04T11:22:00+01:00

这看起来像两个参数2009-05-04T11:22:00和01:00 接收PHP脚本(我无法控制)。

This looks like a two arguments "2009-05-04T11:22:00" and "01:00" to the receiving PHP script (over which I've no control).

NSURL不编码加号,但如果我使用字符串制作NSURL

NSURL doesn't encode plus signs, but if I make an NSURL using the string


2009-05-04T11:22:00%2B01:00

2009-05-04T11:22:00%2B01:00

我最终得到的网址包含:

the url I end up with contains:


2009-05-04T11:22:00%252B01:00

2009-05-04T11:22:00%252B01:00

我有什么想法可以保留我的编码加号或者只是简单地阻止NSURL编码任何东西?

Any ideas how I can preserve my encoded plus sign or just plain prevent NSURL from encoding anything?

推荐答案

对我有用的是UTF8转换,然后用%2B替换+号:

What worked for me was doing the UTF8 conversion, then replacing the + sign with %2B:

NSString *urlString =
    [NSString stringWithFormat:@"%@/iphone/push/create?pn[token]=%@&pn[send_at]=%@",
     kHTTPURL, appDelegate.deviceAPNToken, [dateTimeToUse description]];

urlString =
    [[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
     stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

这篇关于使用编码加(%2B)创建NSURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆