以下是方法 返货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

 

相关文章:

  • 2021-08-29
  • 2021-04-11
  • 2022-12-23
  • 2021-07-14
  • 2021-12-10
  • 2021-10-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
相关资源
相似解决方案