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

标签 objective-c ios json web-services parameters

我正在尝试调用网络服务方法并将参数传递给它。

这是我的网络服务方法:

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

这是我的 Objective-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>

我已经这样解决了,希望对你也有用。

如果您需要更多信息,请引用以下两个问题:
. Add key/value pairs to NSMutableURLRequest
. Request format is unrecognized for URL unexpectedly ending in

关于objective-c - 将参数传递给 Objective C 中的 JSON Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9968042/

相关文章:

objective-c - UISearchDisplayController 显示未过滤的数据

java - 构建嵌套的 JSONObject

ios - 喜欢通过客户端使用两个 API 作为 IOS 应用程序(Swift)的功能实现

javascript - 匹配来自两个单独 JSON feed 的值

c++ - 读取和解析 JSON 文件 C++ BOOST

objective-c - 通过提升的助手和 SMJobBless 获得根访问权限

objective-c - 如何在 Cocoa 中解析 XML?

objective-c - NSUserNotification - 小牛和自定义图像

ios - swift 。导航 Controller 。有条件地更改 subview Controller 的顺序

iphone - 绘制 UITableviewcell 时的动画