如何向现有的POST
NSURLRequest
追加数据?我需要添加一个新参数userId=2323
。
如何向现有的POST
NSURLRequest
追加数据?我需要添加一个新参数userId=2323
。
如果您不想使用第三方类,则以下是设置帖子正文的方法...
NSURL *aUrl = [NSURL URLWithString:@"http://www.apple.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"company=Locassa&quality=AWESOME!";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
只需将您的键/值对附加到POST字符串即可。
在调用NSURLConnection
之前,所有对NSMutableURLRequest
的更改必须完成。
当我复制并粘贴上面的代码并运行TCPMon
时,我发现请求是GET
而不是预期的POST
。
NSURL *aUrl = [NSURL URLWithString:@"http://www.apple.com/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"company=Locassa&quality=AWESOME!";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
关于构建POST
请求的之前的帖子基本上是正确的(将参数添加到body中,而不是URL)。但是如果输入数据中有任何保留字符(例如空格、&和加号),那么您需要处理这些保留字符。换句话说,您应该对输入进行百分号编码。
//create body of the request
NSString *userid = ...
NSString *encodedUserid = [self percentEscapeString:userid];
NSString *postString = [NSString stringWithFormat:@"userid=%@", encodedUserid];
NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:postBody];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//initialize a connection from request, any way you want to, e.g.
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
precentEscapeString
方法的定义如下:
- (NSString *)percentEscapeString:(NSString *)string
{
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}
请注意,曾经有一个可行的NSString
方法,stringByAddingPercentEscapesUsingEncoding
(现在已弃用),它可以做类似的事情,但不要尝试使用它。它处理了一些字符(例如空格字符),但没有处理其他一些字符(例如+
或&
字符)。
当代的等效方法是stringByAddingPercentEncodingWithAllowedCharacters
,但同样不要尝试使用URLQueryAllowedCharacterSet
,因为它也允许+
和&
未经转义地通过。这两个字符在更广泛的“查询”中是允许的,但如果这些字符出现在查询内的值中,则必须进行转义。从技术上讲,您可以使用URLQueryAllowedCharacterSet
来构建可变字符集并删除其中包含的一些字符,或者从头开始构建自己的字符集。
例如,如果您查看Alamofire的参数编码,他们使用URLQueryAllowedCharacterSet
,然后移除generalDelimitersToEncode
(其中包括字符#
、[
、]
和@
,但由于一些旧的Web服务器中存在历史性的bug,?
和/
没有被移除)和subDelimitersToEncode
(即!
、$
、&
、'
、(
、)
、*
、+
、,
、;
和=
)。这是正确的实现(虽然您可以争论是否应该移除?
和/
),但相当复杂。也许CFURLCreateStringByAddingPercentEscapes
更直接/高效。
stringByAddingPercentEscapesUsingEncoding:
是真的,但是 stringByAddingPercentEncodingWithAllowedCharacters:
可以处理每个字符。 例如:URLString = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- Alejandro CotillastringByAddingPercentEncodingWithAllowedCharacters
,但不能使用 URLQueryAllowedCharacterSet
,因为它包括 +
和 &
(这些是更广泛的查询中允许的字符,但如果这些字符出现在查询中的值中,则必须进行转义,而该字符集不会这样做)。因此,您可以使用 URLQueryAllowedCharacterSet
来构建可变字符集并删除其中包含的一些字符,或者从头开始构建自己的字符集。或者使用 CFURLCreateStringByAddingPercentEscapes
。 - RobCFURLCreateStringByAddingPercentEscapes
现已被弃用,您真的应该自己构建适当的字符集。请参见https://stackoverflow.com/a/54742187/1271826。 - Rob NSURL *url= [NSURL URLWithString:@"https://www.paypal.com/cgi-bin/webscr"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"userId=2323";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
上面的示例代码对我非常有帮助,但是(如上所提到的),我认为你需要使用NSMutableURLRequest
而不是NSURLRequest
。在当前形式下,我无法让它响应setHTTPMethod
调用。更改类型可以解决问题。
如果你正在寻找一个快速的解决方案,
let url = NSURL(string: "http://www.apple.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPBody = "company=Locassa&quality=AWESOME!".dataUsingEncoding(NSUTF8StringEncoding)