2010-11-24 16 views

Respuesta

79

No creo que se pueden modificar las cabeceras HTTP de una NSURLRequest. Creo que estás intentando modificar un objeto NSURLRequest que no hayas inicializado.

Se puede crear un mutableCopy de su solicitud y luego configurar los campos de la cabecera con el siguiente método:

-(void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field. 

Después de eso puede copy la solicitud mutable de nuevo en su variable de NSURLRequest.

EDITAR: Añadido siguiente ejemplo

/* Create request variable containing our immutable request 
* This could also be a paramter of your method */ 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]]; 

// Create a mutable copy of the immutable request and add more headers 
NSMutableURLRequest *mutableRequest = [request mutableCopy]; 
[mutableRequest addValue:@"Hless" forHTTPHeaderField:@"X-user-nick"]; 

// Now set our request variable with an (immutable) copy of the altered request 
request = [mutableRequest copy]; 

// Log the output to make sure our new headers are there  
NSLog(@"%@", request.allHTTPHeaderFields); 
+1

Gracias por la respuesta, la Lo que pasa es que no puedes agregar Http Headers a NSURLRequest, solo puedes obtenerlos, para ser sincero, quería usar NSURLRequest porque quería recibir un archivo fragmentado usando el delegado "didRece". iveData "pero ahora estoy usando hilos en su lugar con NSMutableURLRequest. –

+0

Sé que ha pasado un tiempo desde que publiqué esto, pero debido a una demanda algo popular, actualicé la respuesta para corregirla y agregué un ejemplo. – Hless

+0

hey @Hless Intenté este método, pero sigo sin obtener autorización. ¿Puedes ver mi pregunta aquí: http://stackoverflow.com/questions/40342728/sending-httpheaderfield-for-nsurlrequest-in-afmultipartformdata –

5
NSString *urlString = @"http://10.28.79.63:3000/testing"; 

NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] init] ; 
[imageRequest setURL:[NSURL URLWithString:urlString]]; 
[imageRequest setHTTPMethod:@"POST"]; 

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

[imageRequest setValue:contentType forHTTPHeaderField: @"content-Type"]; 
NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; 

//Here u adds the Headers which will be posted in body 

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; content-type: application/octet-stream; name=\"userfile\"; filename=\"inLove.mp4\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[imageRequest setHTTPBody:body]; 
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest 
               delegate:self]; 
7

Ya respondió (y gracias a esas respuestas), pero aquí hay un ejemplo más sencillo:

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; 
[request setValue:@"es" forHTTPHeaderField:@"Accept-Language"]; 
+0

Esta es la respuesta correcta más corta (Y) – Adeel

Cuestiones relacionadas