我在服务器上运行的Web服务以XML格式或JSON格式返回数据.我想要请求JSON格式但使用HTTP Post方法.
任何帮助非常感谢.
提前致谢.
这是用于JSON post请求的代码,TouchJSON Framework用于解析JSON,感谢'schwa'.
NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", @"preference", @"uid", nil]; NSArray *objects = [NSArray arrayWithObjects:@"accuser", @"accpass", @"abc_region", @"100", nil]; NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; NSURL *theURL = [NSURL URLWithString:@"http://url.com/request.php"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f]; [theRequest setHTTPMethod:@"POST"]; [theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"]; NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary]; NSLog(@"%@", theBodyString); NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding]; // NSLog(@"%@", theBodyData); [theRequest setHTTPBody:theBodyData]; NSURLResponse *theResponse = NULL; NSError *theError = NULL; NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError]; NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease]; NSLog(theResponseString); NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseString]; NSLog(@"%@", theResponseDictionary); NSString *theGreeting = [theResponseDictionary objectForKey:@"greeting"]; [self setValue:theGreeting forKey:@"greeting"];