ApiHelper辅助类(Api服务器与MVC的连接辅助方法)

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;

namespace Press_Api1.Models
{
   static public class ApiHelper
    {
        static public string PostApi(string url, string str)
        {
            HttpClient client = new HttpClient();
            HttpContent content = new StringContent(str);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            HttpResponseMessage message = client.PostAsync(url, content).Result;
            string result = message.Content.ReadAsStringAsync().Result;
            return result;
        }
        static public List<T> GetApi<T>(string url)
        {
            HttpClient client = new HttpClient();
            HttpResponseMessage message = client.GetAsync(url).Result;
            string result = message.Content.ReadAsStringAsync().Result;
            List<T> list = JsonConvert.DeserializeObject<List<T>>(result);
            return list;
        }
    }
}
发布了43 篇原创文章 · 获赞 35 · 访问量 1567

猜你喜欢

转载自blog.csdn.net/qq_45244974/article/details/103940049