如何在Windows应用程序中使用restful WCF服务?

时间:2018-03-24 05:50:58

标签: c# .net windows wcf

我正在尝试在Windows应用程序中使用restful WCF服务。请给我任何建议。在jquery ajax我的本地URL http://localhost:4722/Service1.svc/departments/

中使用了restful服务

1 个答案:

答案 0 :(得分:0)

如果您为应用程序使用.NET 4.5或更高版本,则可以使用HttpClient:

using (var httpClient = new HttpClient())
{   
    string response = await httpClient.GetStringAsync(new Uri("http://localhost:4722/Service1.svc/departments/"));

    //handle response
}

否则您可以使用WebClient:

using (var webClient = new WebClient())
{   
    string response = client.DownloadString("http://localhost:4722/Service1.svc/departments/");

    //handle response
}   

两个示例都假设服务URL允许基本GET请求并将数据作为字符串检索。需要进行额外的工作以将数据转换为对象。例如,如果它以JSON形式返回,则需要将JSON与您喜欢的JSON库反序列化。