将parameter passing给Objective C中的JSON Web服务

我试图调用一个web服务方法,并传递给它一个参数。

这是我的web服务方法:

[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void GetHelloWorld() { Context.Response.Write("HelloWorld"); Context.Response.End(); } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void GetHelloWorldWithParam(string param) { Context.Response.Write("HelloWorld" + param); Context.Response.End(); } 

这是我的目标C代码:

 NSString *urlString = @"http://localhost:8080/MyWebservice.asmx/GetHelloWorld"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod: @"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSError *errorReturned = nil; NSURLResponse *theResponse =[[NSURLResponse alloc]init]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; if (errorReturned) { //...handle the error } else { NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", retVal); //...do something with the returned value } 

所以当我打电话给GetHelloWorld时,它工作的很好,

 NSLog(@"%@", retVal); 

显示HelloWorld,但是如何调用GetHelloWorldWithParam? 如何传递参数?

我尝试着:

 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"myParameter" forKey:@"param"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; 

并将以下两行添加到请求中:

 [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody: jsonData]; 

我有错误:

 System.InvalidOperationException: Missing parameter: test. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

感谢您的帮助! 泰迪熊

我已经使用你的代码并修改了一下。 请先尝试以下内容:

  NSString *urlString = @"http://localhost:8080/MyWebservice.asmx/GetHelloWorldWithParam"; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod: @"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSString *myRequestString = @"param="; // Attention HERE!!!! [myRequestString stringByAppendingString:myParamString]; NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]]; [request setHTTPBody: requestData]; 

其余部分与您的代码相同(从NSError *errorReturned = nil行开始)。

现在通常,这个代码应该工作。 但是,如果你没有在你的web.config进行修改,它不会。

检查您的web.config文件是否包含以下行:

 <configuration> <system.web> <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> </system.web> </configuration> 

我已经这样解决了,希望它也适用于你。

如果您需要更多信息,请参考以下2个问题:
。 将键/值对添加到NSMutableURLRequest
。 请求格式无法识别的URL意外结束于

不要手动控制响应stream。 只要改变你的web服务方法,如下所示:

 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetHelloWorld() { return "HelloWorld"; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetHelloWorldWithParam(string param) { return "HelloWorld" + param; } 

如果你只想提供Json,请确保添加了[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 。 但是如果你不添加这个,那么你的方法将能够处理XML和Json请求。

PS确保你的web服务类用[ScriptService]