【发布时间】:2014-03-16 08:58:40
【问题描述】:
我想在 POST 参数中发送 imageUrl,它可以有 &.e.g. imageUrl 可以是http://www.url.com?param1=edfef&param2=erfregreg。
现在,如果我将它直接发送到服务器,那么它不会考虑& 之后的任何参数。 SO上的其他答案建议用%26替换&。但这行不通。因为当我转义整个 POST 参数字符串时,它会将%26 中的% 替换为%25。即http://www.url.com?param1=edfef%2526param2=erfregreg 使网址无效。
转义参数的代码:
NSMutableString *postParams = [NSMutableString stringWithFormat:@"imageUrl=%@&realName=%@, imageUrl, realName];
NSMutableString *escapeParams = (NSMutableString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)postParams, NULL,(CFStringRef)@" !%?$#[]/+:@;*()'\"",kCFStringEncodingUTF8));
self.postParameters = escapeParams;
更新:我尝试了下面的代码,但没有工作。
// URL
NSString * url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=250&height=250", fbId];
NSString *escapedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef) url,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]\" "),
kCFStringEncodingUTF8));
url = escapedString;
// Making REST API call to store user details in table.
NSMutableString *postParams = [NSMutableString stringWithFormat:@"imageUrl=%@&realName=%@", imageUrl, realName];
NSMutableString *escapeParams = (NSMutableString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)postParams, NULL,(CFStringRef)@" !%?$#[]/+:@;*()'\"",kCFStringEncodingUTF8));
postParams = escapeParams;
NSString * getParams = ...;
// Code to call API.
.
. // Setting up url and NSMutableURLRequest
.
NSData *myRequestData = [NSData dataWithBytes:[postParams UTF8String] length:[postParams length]];
[request setHTTPBody:myRequestData];
【问题讨论】:
-
服务器能处理什么?
-
@Wain 请解释您的要求。我不明白。
-
你可以使用身体数据吗?可以使用 JSON 吗?请求中还发布了哪些内容?
-
也许如果您发布您的 POST 代码,我们可以提供更多帮助。与您的代码进行比较。 stackoverflow.com/questions/3566516/…
-
@alexandresoli 我用我试过的代码更新了问题。
标签: ios iphone objective-c nsurlconnection nsurlrequest