使用编码加(%2B)创建NSURL

edo*_*lin 11 iphone url html-encode

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

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

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

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

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

我最终得到的网址包含:

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

任何想法如何保存我的编码加号或只是明确阻止NSURL编码任何东西?

小智 13

对我有用的是进行UTF8转换,然后用%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"];
Run Code Online (Sandbox Code Playgroud)