【问题标题】:+ is not getting added while saving international number保存国际号码时未添加 +
【发布时间】:2013-10-28 12:49:25
【问题描述】:
NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"www.mywebsite.com/setnumber.asmx?myNumber=%@", myNumber.text]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

当我NSLog时,我看到下面的网址

www.mywebsite.com/setnumber.asmx?myNumber=+96512345678

但是当我查看数据库时,我没有看到+。我只看到数字为 96512345678 知道为什么会这样吗?

【问题讨论】:

    标签: ios objective-c web-services nsurlconnection nsurlrequest


    【解决方案1】:

    在 URL 中,+ 表示 (空格)。为了传输字符,您必须对其进行 URL 编码。

    不幸的是,您使用的方法对字符串的 URL 编码不够硬;相反,您需要一些类似的东西:

    NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
      NULL,
      (CFStringRef)unencodedString,
      NULL,
      (CFStringRef)@"!*'();:@&=+$,/?%#[]",
      kCFStringEncodingUTF8
    );
    

    解码使用

    NSString *unencodedUrlString = [@"http://www.demo.com/2%2C7"
    stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    

    【讨论】:

    • 我正在这样做...stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
    • 不幸的是,由于一个错误,该方法做得不够好。
    • 所以我必须使用替换所有功能?并将 + 替换为 %2B
    • 这给我的输出文本为 %2B965%2012345678
    • 任何更新为什么我在编码后在 DB 中获取字符串为%2B965%2012345678这是否意味着在显示数据时,我需要解码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 2014-02-02
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多