2011-02-25 15 views
11

Voglio chiamare il servizio web in C scopo principale che la restituzione di dati XML e devo passare alcune informazioni di intestazione al server proprio come in javascript possiamo farlo utilizzando jQuery chiave,,Come impostare i campi di intestazione Http in Objective-C?

x.setRequestHeader (' ','valore');

dove x è l'oggetto xmlHttpRequest. Come possiamo passare i dati dell'intestazione nella classe NSConnection, Ho usato google ma non ho trovato una buona soluzione. per favore aiutatemi.

risposta

17

È possibile passare le informazioni nell'intestazione utilizzando la classe NSMutableURLRequest e quindi chiamare la classe NSURLConnection (chiamerà il delegato della connessione).

vedere il seguente codice,


NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] 
                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                timeoutInterval:60.0]; 
//do post request for parameter passing 
[theRequest setHTTPMethod:@"POST"]; 

//set the content type to JSON 
[theRequest setValue:@"xml" forHTTPHeaderField:@"Content-Type"]; 

//passing key as a http header request 
[theRequest addValue:@"value1" forHTTPHeaderField:@"key1"]; 

//passing key as a http header request 
[theRequest addValue:@"value2" forHTTPHeaderField:@"key2"]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if(theConnection) 
{ 
    webData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"theConnection is NULL"); 
} 

[theConnection release]; 
+0

Aggiunta custum valore-chiave in NSMutableURLRequest non funziona per URL HTTPS ... Qualche idea ?? – DShah

+0

Gli URL sono HTTP o HTTPS non importa. NSMutableRequest invia l'attributo in entrambi i casi. Gestisci il delegato di autenticazione per il servizio HTTPS? –

+0

Hai affermato di impostare il tipo di contenuto su JSON e ancora di impostarlo su xml? Devi impostarlo su 'application/json' – Houman

Problemi correlati