Estoy intentando llamar a un método de servicio web y pasarle un parámetro.Pasar parámetros a un servicio web JSON en el objetivo C
Aquí es mis métodos de servicio web:
[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();
}
Aquí está mi objetivo código 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
}
Así que cuando llamo GetHelloWorld funciona muy bien y: HelloWorld
NSLog(@"%@", retVal);
pantalla , pero ¿cómo puedo llamar a GetHelloWorldWithParam? Cómo pasar un parámetro?
trato con:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"myParameter" forKey:@"param"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
y añadir las dos líneas siguientes a la solicitud:
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
tengo el 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()
Gracias por su ayuda! Teddy
Hola, muchas gracias, esa es la solución. Olvidé el parámetro ... Mi web.config ya tenía estas líneas. – user1306602