WebAPI的Post接收接口返回json字符串

        public string ReturnKingdeeAccount()
        {
            string responseJson = "";
            try
            {
                string WebAPIUrl = ReturnAppSettingValue("KingdeeAccount"); 

                HttpContent httpContent = new StringContent("");
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpClient httpClient = new HttpClient();

                HttpResponseMessage response = httpClient.PostAsync(WebAPIUrl, httpContent).Result;

                if (response.IsSuccessStatusCode)
                {
                    responseJson =  response.Content.ReadAsStringAsync().Result;
                    responseJson=JsonConvert.DeserializeObject<string>(responseJson);
                }
                else
                {
                    responseJson = "";
                } 
                
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, LogEnum.Error);
                throw ex;
            }
            return responseJson;
        }

  返回的json字符串前台转换json对象可能会失败,故 上述方法最后应改为: return responseJson.Replace("\r\n", "\\r\\n")


posted @ 2017-02-17 10:21  sean-日积月累  阅读(725)  评论(0编辑  收藏  举报