无法从WCF服务获取响应

时间:2013-05-22 00:43:30

标签: objective-c json macos wcf web-services

当我尝试调用在WCF中创建的Web服务时,我遇到了麻烦,此服务是通过Mac OS x app调用的。

这是代码

soapMessage = @"{name:\"Sergio\"}";

NSURL *url = [NSURL URLWithString:@"http://localhost/GSBonoServicios/GSBonoService.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest setHTTPMethod: @"POST"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSData *requestData = [NSData dataWithBytes:[soapMessage UTF8String] length:[soapMessage length]];
[theRequest setHTTPBody:requestData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

连接成功但没有响应,我尝试实现一些教程,我正在寻找Stackoverflow,我找到了很多答案,但没有任何作用,我希望你能帮助我。

PS。我说英语不多,但我希望我解释一下,谢谢

1 个答案:

答案 0 :(得分:0)

最后,我找到了问题在服务中的解决方案,并且有效的代码是

NSString *name=@"Sergio";
NSDictionary *tmp = [[NSDictionary alloc] initWithObjectsAndKeys: name, @"name",
                 nil];
NSURL *url = [NSURL URLWithString:@"http://www.myserver.com/Servicio/ServicioEjemplo.svc/HelloWorld"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod: @"POST"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"applicat ion/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:tmp options:kNilOptions error:&error];
[theRequest setHTTPBody:postdata];
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:postdata
                                             encoding:NSUTF8StringEncoding]);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

由于