0

当我尝试调用在 WCF 中创建的 Web 服务时遇到问题,该服务由 Mac os x 应用程序调用。

这是代码

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,我找到了很多答案,但没有任何效果,我希望你能帮助我。

附言。我不会说很多英语,但我希望我能解释一下,谢谢

4

1 回答 1

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];

谢谢

于 2013-05-22T23:16:30.300 回答