一. 準備工做ide
1. 點擊此訪問 RestSharp 官網,可做參考工具
2. VS2012 中安裝支持.Net4.0的最新版 RestSharp 插件
測試
工具---NuGet程序包管理器---程序包管理器控制檯,輸入以下命令url
Install-Package RestSharp -Version 105.2.3
二. 相關代碼介紹spa
1. 消費REST服務方法,以Json做爲數據格式插件
/// <summary> /// 取得升級服務端的url地址 /// </summary> /// <returns></returns> private String getServerUrl() { String result = ""; UpgraderClient upgraderClient = getUpgraderClient(); if (upgraderClient != null) { result += "http://" + upgraderClient.serverIP + ":" + upgraderClient.serverPort + "/upgraderServer/service/upgrade.do"; } return result; } /// <summary> /// 測試與升級服務端的鏈接 /// </summary> /// <returns></returns> public bool testConnect() { FileRequest fileReq = new FileRequest(); fileReq.type = (int)RequestType.TEST_CONNECT; FileResponse rep = null; try { RestClient client = new RestClient(getServerUrl()); RestRequest request = new RestRequest(Method.POST); request.AddJsonBody(fileReq); RestResponse<FileResponse> response = (RestResponse<FileResponse>)client.Execute<FileResponse>(request); rep = response.Data; } catch { throw new Exception("鏈接遠程服務端失敗!"); } return rep.status == 200; }