如何将参数从iOS发送到ASP.NET WebService?

如何将参数从iOS发送到ASP.NET WebService?,asp.net,ios,xcode,json,web-services,Asp.net,Ios,Xcode,Json,Web Services,我有一个用c#编写的web服务,它如下所示: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { return "Hello World"; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public

我有一个用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;
    }
 - (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);


    }
}
我在Xcode中的代码如下所示:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloParam(string param)
    {
        return "Hello " + param;
    }
 - (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 web服务之间创建连接和通信吗?提前感谢

您需要发送一个json,格式为
'{“param”:“paramValue”}'
。我没有关于iOs编程的相关知识,但我看到您可以使用类似以下内容设置post参数
[请求setPostValue:@“{\”param\:\“YOUR\\ param\\”}”forKey:@“param”]您的解决方案听起来不错,但我无法编写参数setPostValue。我对iOS编程的知识非常有限,因此我恐怕无法在这方面提供太多帮助。为什么不能编写参数?您编写的参数在iOS中不存在。从这里获得该代码片段: