C# 请求Web Api 接口,返回的json数据直接反序列化

页面导航:首页 > 网络编程 > JavaScript > C# 请求Web Api 接口,返回的json数据直接反序列化

C# 请求Web Api 接口,返回的json数据直接反序列化

来源: 作者: 时间:2016-02-20 09:49 【

需要的引用的dll类:Newtonsoft Json dll、System Net Http dll、System Net Http Formatting dllWeb Api接口为GET形式: public static CstyleCmappListRespDTO GetCstyleCmappList(string cst

需要的引用的dll类:

Newtonsoft.Json.dll、System.Net.Http.dll、System.Net.Http.Formatting.dll



Web Api接口为GET形式:

 public static CstyleCmappListRespDTO GetCstyleCmappList(string cstylename, string cmappgname)
        {
            CstyleCmappListRespDTO RespDTO = new CstyleCmappListRespDTO();
            var url = string.Empty;
            try
            {
                url = RequestUrl + "/dw/get_cstylecmappg_info_dw?id={0}&key={1}×tamp={2}&cstylename={3}&cmappgname={4}";
                url = string.Format(url, id, key, DateTime.Now.ToString("yyyyMMddHHmmss"), cstylename, cmappgname);
                var httpClient = new HttpClient();
                HttpResponseMessage response = httpClient.GetAsync(new Uri(url)).Result;
                RespDTO = response.Content.ReadAsAsync().Result;
            }
            catch (Exception ex)
            {
                
            }
            return RespDTO;
        }

Web Api接口为POST形式:

public static FlightsResponse QueryFlightPost(FlightQueryRequest ReqDTO)
        {
            FlightsResponse FlightsRespDTO = new FlightsResponse();
            try
            {
                string requestDTO = JsonConvert.SerializeObject(ReqDTO);
                MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
                HttpContent httpContent = new ObjectContent(ReqDTO, jsonFormatter);
                var url = RequestUrl + "/QueryFlight";
                var httpClient = new HttpClient(new RequestHandler() { InnerHandler = new HttpClientHandler() });
                httpClient.Timeout = TimeSpan.FromMinutes(3);
                var responseJson = httpClient.PostAsync(url, httpContent).Result;
                FlightsRespDTO = responseJson.Content.ReadAsAsync().Result;
                
            }
            catch (Exception)
            {
                FlightsRespDTO = new FlightsResponse();
            }
            return FlightsRespDTO;
        }







Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<