2013-07-05 6 views

Répondre

23

Est-il possible d'appeler une méthode Web Api à partir d'un client .NET 2.0?

Bien sûr que c'est possible. Vous pouvez l'appeler à partir de n'importe quel client compatible HTTP. Le client peut même ne pas être .NET.

Par exemple, dans .NET 2.0, vous pouvez utiliser la classe 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 
} 

et si vous vouliez une valeur POST:

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 
} 
+0

oui, mais à la fin nous avons besoin de convertir octet réponse à la chaîne à nouveau pour afficher la réponse appropriée –

Questions connexes