2013-07-31 14 views
1

我打電話給iOS中的Web服務。爲此,我需要在NSMutableURLRequest對象中設置標題。我的服務接受兩個字符串參數並以JSON格式返回數據。 我需要使用setValue:forHTTPHeaderField:在標題中設置哪些字段(兩者都使用GETPOST)。要在HTTP標頭中設置的字段

在使用GET時,我們不需要使用setHTTPBody:

回答

0

看看這個代碼,我使用的調用Web服務和那爲我工作

+(NSString *)http_post_method_changed:(NSString *)url content:(NSString *)jsonContent{ 
NSURL *theURL = [NSURL URLWithString:url]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f]; 
NSData *requestData = [jsonContent dataUsingEncoding:NSUTF8StringEncoding]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
[theRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPBody: requestData]; 
NSURLResponse *theResponse = NULL; 
NSError *theError = NULL; 
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError]; 
NSString *data=[[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding]; 
NSLog(@"url to send request= %@",url); 
NSLog(@"response1111:-%@",data); 
return data; 
} 
0

兩者同時使用GET和POST我們不需要使用setHTTPBody?
setHTTPBody只有在使用POST請求時纔有意義。 GET不需要它。

對於頭參數做類似下面使用HTTP POST方法時

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f]; 

//Specify method of request(Get or Post) 
[theRequest setHTTPMethod:@"GET"]; 

//Pass some default parameter(like content-type etc.) 
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 

//Now pass your own parameter 

[theRequest setValue:yourObj forHTTPHeaderField:@"your parameter name"]; 
0

setHTTPBody被使用。

NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0]; 

[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:data]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

這裏的數據只是需要發送的JSON數據的NSData。

4

我有同樣的問題,我解決了(使用Iducool和ANKIT梅塔一些代碼)這樣​​的...

NSURL *theURL = [NSURL URLWithString:@"yourURL"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL  cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f]; 

//Specify method of request(Get or Post) 
[theRequest setHTTPMethod:@"GET"]; 

//Pass some default parameter(like content-type etc.) 
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 

//Now pass your own parameter 

[theRequest setValue:yourValue forHTTPHeaderField:theNameOfThePropertyValue]; 

NSURLResponse *theResponse = NULL; 
NSError *theError = NULL; 
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError]; 

//Now you can create a NSDictionary with NSJSONSerialization 
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:0 error:&theError]; 
NSLog(@"url to send request= %@",theURL);  
NSLog(@"%@",dataDictionaryResponse); 
+0

我應該在哪裏把我的令牌代碼 –