c# .net web-services asp.net-web-api .net-2.0

c# - Llamando al servicio Web Api desde un cliente.NET 2.0



web-services asp.net-web-api (1)

¿Es posible llamar a un método Web Api desde un cliente .NET 2.0?

Refiriéndose a la guía aquí: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-f-a-net-client

Algunos de estos dlls para el cliente no parecen ser compatibles con .NET 2.0

¿Hay alguna forma de llamar a un método Web Api desde un cliente .NET 2.0 sin agregar ningún dlls?


¿Es posible llamar a un método Web Api desde un cliente .NET 2.0?

Por supuesto que es posible. Puede llamar desde absolutamente cualquier cliente compatible con HTTP. El cliente puede que ni siquiera sea .NET.

Por ejemplo, en .NET 2.0 podría usar la clase WebClient :

using (var client = new WebClient()) { client.Headers[HttpRequestHeaders.Accept] = "application/json"; string result = client.DownloadString("http://example.com/values"); // now use a JSON parser to parse the resulting string back to some CLR object }

y si quería POSTAR algún valor:

using (var client = new WebClient()) { client.Headers[HttpRequestHeader.ContentType] = "application/json"; client.Headers[HttpRequestHeader.Accept] = "application/json"; var data = Encoding.UTF8.GetBytes("{/"foo/":/"bar/"}"); byte[] result = client.UploadData("http://example.com/values", "POST", data); // now use a JSON parser to parse the resulting string back to some CLR object }