小编典典

将参数传递给Objective C中的JSON Web服务

json

我正在尝试调用webservice方法并将参数传递给它。

这是我的网络服务方法:

    [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()

谢谢您的帮助!泰迪熊


阅读 263

收藏
2020-07-27

共1个答案

小编典典

我已经使用了您的代码并进行了一些修改。请先尝试以下操作:

 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>

我已经通过这种方式解决了它,希望它也对您有用。

2020-07-27