Reputation: 227
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
Upvotes: 5
Views: 16194
Reputation: 5330
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
Upvotes: 2
Reputation: 68187
Don't take over control of response stream manually. Just change your webservice method a little as below:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetHelloWorld()
{
return "HelloWorld";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetHelloWorldWithParam(string param)
{
return "HelloWorld" + param;
}
Make sure you add [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
if you only want to offer Json in return. But if you dont add this, then your method will be capable of handling both XML and Json request.
P.S. Make sure your web service class is decorated with [ScriptService]
Upvotes: 1