3

我正在向服务器发送请求,NSMutableURLRequest如下所示,问题之一是电话号码并带有“+”号的参数已被删除。

// Create the request.
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:myURL ]
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                        timeoutInterval:30.0];
    /**
     add post
     ****/
    [theRequest  setHTTPMethod:@"POST"];

    NSString *encodedfullNumberFinal= [ fullNumberFinal stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *append2URL=[NSString stringWithFormat:@"deviceType=%d&token=%@&mobileNumber=%@&code=%@",
                          1, token,

                          encodedfullNumberFinal
                          ,myBid
                          ];



    [theRequest setHTTPBody:[append2URL

                             dataUsingEncoding:NSUTF8StringEncoding /*NSUnicodeStringEncoding*/]];


    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = /*[*/[NSMutableData data] /* retain]*/;
    } else {
        // Inform the user that the connection failed.

    }

注意,形成服务器端我使用的是 Tomcat 和 Java。
有什么建议如何用“+”号保存参数?

4

2 回答 2

5

问题是该标准stringByAddingPercentEscapesUsingEncoding对加号没有任何作用。不过,Core FoundationCFURLCreateStringByAddingPercentEscapes为您提供了更多控制权。我使用一个NSString提供简单接口的类别CFURLCreateStringByAddingPercentEscapes

@implementation NSString (UrlEncode)

+ (NSString *)stringByAddingPercentEscapesFor:(NSString *)string
                legalURLCharactersToBeEscaped:(NSString *)legalURLCharactersToBeEscaped
                                usingEncoding:(NSStringEncoding)encoding
{
    return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                     (CFStringRef)string,
                                                                     NULL,
                                                                     (CFStringRef)legalURLCharactersToBeEscaped,
                                                                     CFStringConvertNSStringEncodingToEncoding(encoding)
                                                                     ));
}

- (NSString *)stringByAddingPercentEscapesFor:(NSString *)legalURLCharactersToBeEscaped
                                usingEncoding:(NSStringEncoding)encoding
{
    return [NSString stringByAddingPercentEscapesFor:self
                       legalURLCharactersToBeEscaped:legalURLCharactersToBeEscaped
                                       usingEncoding:encoding];
}

- (NSString *)stringByAddingPercentEscapesForURLParameterUsingEncoding:(NSStringEncoding)encoding
{
    return [NSString stringByAddingPercentEscapesFor:self
                       legalURLCharactersToBeEscaped:@":/?@!$&'()*+,;="
                                       usingEncoding:encoding];
}

@end

然后我可以使用stringByAddingPercentEscapesForURLParameterUsingEncoding而不是stringByAddingPercentEscapesUsingEncoding,加号也会被转义。

于 2013-06-15T21:54:46.323 回答
1

Yap 在 URL 中+表示一个空格。您必须明确将其包含在要编码的字符列表中。

NSString *encodedfullNumberFinal = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (__bridge CFStringRef)fullNumberFinal,
    CFSTR(""),
    CFSTR("+"), // you may want to include more valid URL characters here too
    kCFStringEncodingUTF8,
);
于 2013-06-15T21:55:22.657 回答