调用接口获取对象集合

 

//将字典 对象 转json

/// <summary>
/// 类对像转换成json格式
/// </summary>
/// <returns></returns>
public static string ToJson(object t)
{
return JsonConvert.SerializeObject(
t, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }
);
}

 

//调用接口 获取对象

 ResultVo<model类> result = ZHttpUtil.PostAndRespSignle<ResultVo<model类>>(url, postData: body);

 

internal class ResultVo<T>
{
public int code { get; set; }

public string msg { get; set; }

public T obj { get; set; }
}

 

 /// <summary>

        /// Post Http请求

        /// </summary>

        /// <param name="url">请求地址</param>

        /// <param name="postData">传输数据</param>

        /// <param name="timeout">超时时间</param>

        /// <param name="contentType">媒体格式</param>

        /// <param name="encode">编码</param>

        /// <returns>泛型集合</returns>

        public static T PostAndRespSignle<T>(string url, int timeout = 30000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") where T : class

        {

            if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)

            {

                // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData");

                HttpWebResponse webResponse = null;

                Stream responseStream = null;

                Stream requestStream = null;

                StreamReader streamReader = null;

                try

                {

                    string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);

                    return ZJsonHelper.JsonToObj<T>(respstr);

                }

                catch (Exception ex)

                {

                    Commons.LogHelper.WriteErrLog("【登陆请求】(PostAndRespSignle)登陆失败," + ex.Message, ex);

                }

                finally

                {

                    if (responseStream != null)

                    {

                        responseStream.Dispose();

                    }

 

                    if (webResponse != null)

                    {

                        webResponse.Close();

                    }

 

                    if (requestStream != null)

                    {

                        requestStream.Dispose();

                    }

 

                    if (streamReader != null)

                    {

                        streamReader.Dispose();

                    }

                }

            }

            return default(T);

        }

 /// <summary>        /// Post Http请求        /// </summary>        /// <param name="url">请求地址</param>        /// <param name="postData">传输数据</param>        /// <param name="timeout">超时时间</param>        /// <param name="contentType">媒体格式</param>        /// <param name="encode">编码</param>        /// <returns>泛型集合</returns>        public static T PostAndRespSignle<T>(string url, int timeout = 30000, string postData = "", string contentType = "application/json;", string encode = "UTF-8") where T : class        {            if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)            {                // webRequest.Headers.Add("Authorization", "Bearer " + "SportApiAuthData");                HttpWebResponse webResponse = null;                Stream responseStream = null;                Stream requestStream = null;                StreamReader streamReader = null;                try                {                    string respstr = GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);                    return ZJsonHelper.JsonToObj<T>(respstr);                }                catch (Exception ex)                {                    Commons.LogHelper.WriteErrLog("【登陆请求】(PostAndRespSignle)登陆失败," + ex.Message, ex);                }                finally                {                    if (responseStream != null)                    {                        responseStream.Dispose();                    }
                    if (webResponse != null)                    {                        webResponse.Close();                    }
                    if (requestStream != null)                    {                        requestStream.Dispose();                    }
                    if (streamReader != null)                    {                        streamReader.Dispose();                    }                }            }            return default(T);        }

posted @ 2020-10-15 10:35  张汐  阅读(217)  评论(0)    收藏  举报