//定義參數 C#調取webapi var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"Id",Guid.Empty.ToString()}, {"data",DateTime.Now.ToString("yyyy-MM-dd") }, {"new_visitor","123"}, {"new_visitor_percent","20.00%"}, {"old_visitor","456"}, {"old_visitor_percent","30.00%"}, {"orgid","5e436943-2e51-4d34-baab-762cf58b2ac7"}, }); result = RequestAPI(content, "api/SiteService/GetSaveCtrUserCompare"); public string RequestAPI(FormUrlEncodedContent content, string strmethod) { //提交當前地址的webapi //string apiUrl = ConfigurationManager.AppSettings["SSOPassport"]; string apiUrl = "http://xxx.xxx.xx.xxx:8077/"; //向用戶中心提交部門 //後臺client方式GET提交 HttpClient myHttpClient = new HttpClient(); myHttpClient.BaseAddress = new Uri(apiUrl); HttpResponseMessage response = myHttpClient.PostAsync(strmethod, content).Result; string result = ""; if (response.IsSuccessStatusCode) { result = response.Content.ReadAsStringAsync().Result; } //將json字符串轉化爲對應的格式 JObject jsonObj = JObject.Parse(result); string dataStr = string.Empty; if (jsonObj.Property("Result") != null && jsonObj["Result"] != null && !string.IsNullOrEmpty(jsonObj["Result"].ToString())) { dataStr = jsonObj["Result"].ToString(); } return dataStr; }