将数据追加到POST NSURLRequest

如何将数据追加到现有的POST NSURLRequest ? 我需要添加一个新的参数userId=2323

如果你不想使用第三方类,那么下面是你如何设置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]; 

只需将您的键/值对附加到后string

在调用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请求的post很大程度上是正确的(将参数添加到正文中,而不是URL)。 但是,如果input数据中有任何保留字符(例如空格,&符号,加号)的机会,那么您将需要处理这些保留字符。 也就是说,你应该百分之一 – 逃避投入。

 //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, eg 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来构build一个可变字符集,并删除其中包含的一些字符,或从头开始构build自己的字符集。

例如,如果你看Alamofire的参数编码 ,他们采取URLQueryAllowedCharacterSet ,然后删除generalDelimitersToEncode (其中包括字符#[]@ ,但由于一些旧的Web服务器的历史错误,既不? subDelimitersToEncode (即!$&'()*+ subDelimitersToEncode= )。 这是正确的实现(虽然你可以辩论删除?/ ),虽然相当复杂。 也许CFURLCreateStringByAddingPercentEscapes更直接/更高效。

  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 。 在目前的forms下,我无法让它响应setHTTPMethod调用。 改变types固定的东西了。

任何一个寻找快速解决scheme

 let url = NSURL(string: "http://www.apple.com/") let request = NSMutableURLRequest(URL: url!) request.HTTPBody = "company=Locassa&quality=AWESOME!".dataUsingEncoding(NSUTF8StringEncoding)