3

以下のようにサーバーにリクエストを送信してNSMutableURLRequestいます。電話番号で「+」記号を持つパラメーターの問題の1つが削除されます。

// 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

The problem is that the standard stringByAddingPercentEscapesUsingEncoding doesn't do anything with plus signs. The Core Foundation CFURLCreateStringByAddingPercentEscapes gives you more control, though. And I use a NSString category that provides a simple interface to 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

I can then use stringByAddingPercentEscapesForURLParameterUsingEncoding rather than stringByAddingPercentEscapesUsingEncoding, and the plus sign will be escaped, too.

于 2013-06-15T21:54:46.323 に答える