如何在将URL请求发送到Web服务器时处理+字符

时间:2011-02-07 11:30:06

标签: iphone objective-c

我通过向服务器发送请求来解决问题,其中包括必须包含+字符的电话号码的详细信息。然后+字符不在服务器端显示。

curl = [curl stringByAppendingString:@"name="]; 
    curl = [curl stringByAppendingString:profileObj.name];
    curl = [curl stringByAppendingString:@"&email="];
    curl = [curl stringByAppendingString:profileObj.email];
    curl = [curl stringByAppendingString:@"&password="];
    curl = [curl stringByAppendingString:profileObj.password];
    curl = [curl stringByAppendingString:@"&phoneno="];
    curl = [curl stringByAppendingString:profileObj.phoneno]; // here my data is +919999999999
    curl = [curl stringByAppendingString:@"&address="];
    curl = [curl stringByAppendingString:profileObj.address];
    curl = [curl stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    NSString *escapedUrlString = [curl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *finalURL = [NSURL URLWithString:escapedUrlString];

    [self updateStatus];
    if (internetConnectionStatus == NotReachable) 
    {
        UIAlertView *reachbleAlert = [[UIAlertView alloc] initWithTitle:@"Network Error"
                                                   message:@"No Network Available. \n This Application requires network connectivity. "
                                                  delegate:self 
                                         cancelButtonTitle:@"Ok"
                                         otherButtonTitles: nil];
        [reachbleAlert show];   
        [reachbleAlert release];
        return;
    }
    else 
    {

        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL 
                                                                  cachePolicy:NSURLRequestReloadIgnoringCacheData 
                                                              timeoutInterval:10];
        [theRequest setHTTPMethod:@"POST"];

        [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];

        [pool release];

    }

我用下面的行:

 NSString *escapedUrlString = [curl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

在请求服务器之前。所以请帮我处理+字符的任何解决方案。

1 个答案:

答案 0 :(得分:0)

+是网址中的合法字符,因此stringByAddingPercentEscapesUsingEncoding:会忽略它。您应该在将+符号放入网址之前手动转义{1}}符号,例如

...
curl = [curl stringByAppendingString:@"&phoneno="];
curl = [curl stringByAppendingString: [profileObj.phoneno stringByReplacingOccurencesOfString: @"+" with: @"%2B"]]; // here my data is +919999999999 
...

NB不确定%2B。 2B或2B,这就是问题。