问题描述
我正在尝试调用网络服务方法并向其传递参数.
这是我的网络服务方法:
[WebMethod][ScriptMethod(ResponseFormat = ResponseFormat.Json)]public void GetHelloWorld(){Context.Response.Write("HelloWorld");Context.Response.End();}[网络方法][ScriptMethod(ResponseFormat = ResponseFormat.Json)]公共无效GetHelloWorldWithParam(字符串参数){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];[请求 setHTTPMethod:@"POST"];[请求 setValue:@"application/json" forHTTPHeaderField:@"Accept"];[请求 setValue:@"application/x-www-form-urlencoded" for HTTPHeaderField:@"Content-Type"];NSError *errorReturned = nil;NSURLResponse *theResponse =[[NSURLResponse alloc]init];NSData *data = [NSURLConnection sendSynchronousRequest:请求返回响应:&theResponse 错误:&errorReturned];如果(错误返回){//...处理错误}别的{NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@", retVal);//...对返回的值做一些事情}
因此,当我调用 GetHelloWorld 时,它运行良好,并且:
NSLog(@"%@", retVal);
显示 HelloWorld,但如何调用 GetHelloWorldWithParam ?如何传递参数?
我尝试:
NSMutableDictionary *dict = [NSMutableDictionary 字典];[dict setObject:@"myParameter" forKey:@"param"];NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
并将以下两行添加到请求中:
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];[请求 setHTTPBody: jsonData];
我有错误:
System.InvalidOperationException:缺少参数:测试.在 System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection 集合)在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
感谢您的帮助!泰迪
我已经使用了您的代码并稍作修改.请先尝试以下操作:
NSString *urlString = @"http://localhost:8080/MyWebservice.asmx/GetHelloWorldWithParam";NSURL *url = [NSURL URLWithString:urlString];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];[请求 setHTTPMethod:@"POST"];[请求 setValue:@"application/json" forHTTPHeaderField:@"Accept"];[请求 setValue:@"application/x-www-form-urlencoded" for HTTPHeaderField:@"Content-Type"];NSString *myRequestString = @"param=";//注意这里!!![myRequestString stringByAppendingString:myParamString];NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] 长度:[myRequestString 长度]];[请求 setHTTPBody: requestData];
其余部分与您的代码相同(从 NSError *errorReturned = nil
行开始).
现在正常情况下,这段代码应该可以工作了.但是,如果您没有在 web.config
中进行以下修改,则不会.
检查您的 web.config
文件是否包含以下几行:
我已经这样解决了,希望对你也有用.
如果您需要更多信息,请参考以下两个问题:
.将键/值对添加到 NSMutableURLRequest
.无法识别以
I'm trying to call a webservice method and pass a parameter to it.
Here is my webservice methods:
[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();
}
Here is my objective c code:
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
}
So when I call GetHelloWorld it works great and:
NSLog(@"%@", retVal);
display HelloWorld, but how do I call GetHelloWorldWithParam ? How to pass a parameter ?
I try with:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"myParameter" forKey:@"param"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
and add the two following lines to the request:
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
I have the error :
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()
Thank you for your help!Teddy
I have used your code and modified a bit. Please try following first:
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];
Rest part is same with your code (starting from the line NSError *errorReturned = nil
).
Now normally, this code should work. But if you haven't make the modification below in your web.config
, it won't.
Check if your web.config
file includes following lines:
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
I have solved it this way, hope it works for you too.
If you need more informations, please refer these 2 following questions:
. Add key/value pairs to NSMutableURLRequest
. Request format is unrecognized for URL unexpectedly ending in
这篇关于将参数传递给 Objective C 中的 JSON Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!