C# 后台获取WebApi 方法

以下是方法 返货json数据 代码都有注释

/// <summary>
        /// 请求webpapi
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns>json字符串</returns>
        public static string GetJosn(string url)
        {
            string result = "";
            using (var client = new System.Net.Http.HttpClient())
            {
                //通过客服端地址访问
                Uri bUrl= client.BaseAddress = new Uri(url);
                //序列化成json
                var requestJson = JsonConvert.SerializeObject(bUrl);
                //创建一个http 带正文和内容
                HttpContent httpContent = new StringContent(requestJson);
                //设置数据格式为json
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                //异步请求并将内容写入文件流中 返回字符串
                result = client.PostAsync("api/Product/AddProduct", httpContent).Result.Content.ReadAsStringAsync().Result;

}
            return result;
        }

或则参考地址 http://www.2cto.com/kf/201408/326364.html

上一篇:Unix/Linux环境C编程入门教程(15) BT5开发环境搭建


下一篇:vue 开发环境搭建