1

我正在调用 iOS 中的 Web 服务。为此,我需要在NSMutableURLRequest对象中设置标题。我的服务接受两个字符串参数并以 JSON 格式返回数据。我需要GET使用POST.setValue:forHTTPHeaderField:

我们不需要在使用setHTTPBody:时使用GET..对吗???

4

4 回答 4

4

我有同样的问题,我像这样解决了(使用来自 Iducool 和 Ankit Mehta 的一些代码)......

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);
于 2014-02-02T18:16:50.410 回答
0

当使用 HTTP 方法 POST 时使用 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。

于 2013-07-31T09:19:54.840 回答
0

看看我用来调用网络服务的这段代码,这对我有用

+(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;
}
于 2013-07-31T08:31:02.720 回答
0

同时使用 GET 和 POST 我们不需要使用 setHTTPBody 吗?
setHTTPBody 仅在您使用 POST 请求时才有意义。GET 不需要它。

对于 Header 参数,请执行以下操作

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"];
于 2013-07-31T08:42:39.230 回答