use*_*602 5 parameters json web-services objective-c ios
我正在尝试调用webservice方法并将参数传递给它.
这是我的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();
}
Run Code Online (Sandbox Code Playgroud)
这是我的目标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
}
Run Code Online (Sandbox Code Playgroud)
因此,当我致电GetHelloWorld时,它的效果非常好:
NSLog(@"%@", retVal);
Run Code Online (Sandbox Code Playgroud)
显示HelloWorld,但如何调用GetHelloWorldWithParam?如何传递参数?
我试着用:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"myParameter" forKey:@"param"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
Run Code Online (Sandbox Code Playgroud)
并将以下两行添加到请求中:
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
Run Code Online (Sandbox Code Playgroud)
我有错误:
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()
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!泰迪熊
我使用了你的代码并做了一些修改。请先尝试以下操作:
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];
Run Code Online (Sandbox Code Playgroud)
其余部分与您的代码相同(从行开始NSError *errorReturned = nil)。
现在正常情况下,这段代码应该可以工作。但如果您没有在您的 中进行以下修改web.config,则不会。
检查您的web.config文件是否包含以下行:
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我已经用这种方法解决了,希望它也适用于你。
如果您需要更多信息,请参考以下 2 个问题:
。将键/值对添加到 NSMutableURLRequest
。由于 URL 意外结尾,因此无法识别请求格式
| 归档时间: |
|
| 查看次数: |
16028 次 |
| 最近记录: |