objective c - saber - Solicitud de publicación HTTP en Objective-C no funciona
monarquias actuales 2018 (1)
Estoy escribiendo una solicitud HTTP Post, pero por alguna razón los parámetros no se están agregando correctamente, y no puedo por la vida de entender qué estoy haciendo mal. Esto es lo que tengo:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"text; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// Dictionary that holds post parameters.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:subject forKey:@"subject"];
[_params setObject:message forKey:@"message"];
[_params setObject:[[UIDevice currentDevice] systemName] forKey:@"device"];
// add params
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:@"--%@/r/n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=/"%@/"/r/n/r/n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@/r/n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// the server url
NSURL* requestURL = [NSURL URLWithString:CIVCManifest.contactFeed];
[body appendData:[[NSString stringWithFormat:@"--%@--/r/n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:requestURL];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
La solicitud se está completando, sin embargo, todos los parámetros no se están agregando correctamente. Tenía un script de Post trabajando que subiría una foto, y copié y pegué la mayor parte de ella en esta, pero de alguna manera esta no funciona. Espero que sea solo un error simple que me estoy perdiendo.
Prueba esto
NSString *Post = [[NSString alloc] initWithFormat:@"Post Parameters"];
NSURL *Url = [NSURL URLWithString:@"Url"];
NSData *PostData = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [PostData length]];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] init];
[Request setURL:Url];
[Request setHTTPMethod:@"POST"];
[Request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[Request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[Request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[Request setHTTPBody:PostData];