簡體   English   中英

如何在Windows應用程序中使用靜態的WCF服務?

[英]How to consume a restful WCF service in a Windows application?

我正在嘗試在Windows應用程序中使用靜態的WCF服務。 請給我任何建議。 靜態服務在jquery ajax中消耗,我的本地URL http:// localhost:4722 / Service1.svc / departments /

如果您為應用程序使用.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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM