14

Is it possible to call a Web Api method from a .NET 2.0 client?

Referring to the guide here: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

Some of these dlls for the client doesn't seem to be compatible with .NET 2.0

Are there any ways to call a Web Api method from a .NET 2.0 client without adding any dlls?

1 Answer 1

28

Is it possible to call a Web Api method from a .NET 2.0 client?

Of course it's possible. You can call it from absolutely any HTTP compliant client. The client might not even be .NET.

For example in .NET 2.0 you could use the WebClient class:

using (var client = new WebClient())
{
    client.Headers[HttpRequestHeader.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
}

and if you wanted to POST some value:

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);
    string resultContent = Encoding.UTF8.GetString(result, 0, result.Length);        
    //now use a JSON parser to parse the resulting string back to some CLR object
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, but in the end we need to convert byte response to string again to display proper response

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.