gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:44:40 25 4
gpt4 key购买 nike

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

这是我的网络服务方法:

    [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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com