如何为GET请求编码url字符串?我特别遇到了一个问题,我想发送的字段的值包含&符号(&),它被解释为字段之间的分隔符。如何使用NSMutableURLRequest对GET请求中的特殊字符进行编码
我目前正在创建我的要求如下(这实际上使呼叫从苹果的MVC组网示例所采取的代码,所以它可能是不相关的这个问题,但为了保持完整性)
NSString *urlString = [[NSString alloc] initWithFormat:@"http://.../?name=%@&nameupdated=%@", name, nameUpdated];
NSMutableURLRequest *request;
NSString *webStringUrl = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url;
url = [NSURL URLWithString:webStringUrl];
assert(url != nil);
// Call down to the network manager so that it can set up its stuff
if (url != nil) {
request = [[NetworkManager sharedManager] requestToGetURL:url];
assert(request != nil);
}
RetryingHTTPOperation *updateOperation = [[[RetryingHTTPOperation alloc] initWithRequest:request] autorelease];
assert(updateOperation != nil);
[updateOperation setQueuePriority:NSOperationQueuePriorityNormal];
[[NetworkManager sharedManager] addNetworkManagementOperation:updateOperation finishedTarget:self action:@selector(updateItemOperationDone:)];
[urlString release];
如果名称字段是像“床&早餐”,那么URL字符串结束为: http://.../?name=Bed%20&%20Breakfast& ....
和我结束了唯一的“床”是我的名字。
NSString *urlEncoded = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)sourceString,
NULL,
(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
kCFStringEncodingUTF8);
然后构建从单独的URL编码片段的完整URL字符串:
嗨..For我,我有编码+在发送请求。我已经通过去除除了+这样的所有字符发送请求..的NSString *了urlencoded =(的NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)sourceString, NULL, (CFStringRef)@ “+”, kCFStringEncodingUTF8);.对某人有帮助,并感谢Ole Begemann。重要的是这是为ARC启用的项目。 –