我有一个用c#编写的Web服务,如下所示:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloParam(string param)
{
return "Hello " + param;
}
我在Xcode中的代码如下所示:
- (IBAction)getButton:(id)sender {
NSString *urlString = @"http://172.16.43.132/Server/Service1.asmx/HelloParam";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod: @"POST"];
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(@"Shit");
}
else
{
NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", retVal);
}
}
当我调用方法hello world时。我收到xml格式的答案。它应该在json中。
当我调用第二种方法“helloparam”时出现错误:参数丢失。
你能给我一个如何在iPhone和asp.net网络服务之间建立连接和通信的简单例子吗?提前谢谢