zoukankan      html  css  js  c++  java
  • ABP框架中微服务跨域调用其它服务接口

    AjaxResponse为ABP自动包装的JSON格式
        /// <summary>
        /// 通过地址和参数取得返回OutPut数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public static OutPut ResultGet(string url, object input = null)
        {
            var data = Get(url, input);
            var response = data.JsonToObject<AjaxResponse>();
    
            if (response.Success == false)
            {
                var str = data.JsonToObject<AjaxResponse>();
                throw new Exception(str.Error.Message);
            }
            return response.Result.ToString().JsonToObject<OutPut>();
        }
    
        /// <summary>
        /// 通过地址和参数取得返回OutPut数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public static OutPut ResultPost(string url, object input = null)
        {
            var data = Post(url, input);
            var response = data.JsonToObject<AjaxResponse>();
    
            if (response.Success == false)
            {
                var str = data.JsonToObject<AjaxResponse>();
                throw new Exception(str.Error.Message);
            }
            return response.Result.ToString().JsonToObject<OutPut>();
        }
    
        /// <summary>
        ///通过地址和我反序列后的JSON取得返回GeneralOutPut数据
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="input">请求参数</param> 
        /// <returns>请求返回的结果</returns>
        private static string Get(string url, object input = null)
        {
            var properties = input?.GetType().GetProperties();
            var parm = string.Empty;
            foreach (var propertie in properties)
            {
                var name = propertie.Name;
                var value = input.GetType().GetProperty(name).GetValue(input);//直接根据属性的名字获取其值  
    
                parm += $"&{ name}={value}";
            }
    
            parm = parm.Trim('&');
            url = $"{url}?{parm}";
    
            // Prepare web request...  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/json";
    
            return HttpRequest(request);
        }
    
        /// <summary>  
        /// 指定Post地址使用Get 方式获取全部字符串  
        /// </summary>  
        /// <param name="url">请求后台地址</param>  
        /// <returns></returns>  
        public static string Post(string url, object input)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
    
            #region 添加Post 参数  
            var parm = Serialization.JsonSerialize(input);
            byte[] data = Encoding.UTF8.GetBytes(parm);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
    
            return HttpRequest(req);
        }
    
        /// <summary>
        /// 请求的主体部分(由此完成请求并返回请求结果)
        /// </summary>
        /// <param name="request">请求的对象</param> 
        /// <returns>请求返回结果</returns>
        private static string HttpRequest(HttpWebRequest request)
        {
            HttpWebResponse response = null;
    
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
    
            string result = string.Empty;
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
    
            return result;
        }
    
        /// <summary>
        /// URL拼写完整性检查
        /// </summary>
        /// <param name="url">待检查的URL</param>
        private static string UrlCheck(string url)
        {
            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
                url = "http://" + url;
    
            return url;
        }
  • 相关阅读:
    网络安全专家教你设置史上最安全的WiFi密码
    Python语言为什么被称为高级程序设计语言?
    常用组件
    小程序路由
    小程序的生命周期函数
    小程序案例-查询天气
    第一个小程序,获取用户名和用户头像
    小程序util.js的使用
    小程序文件夹目录分析 转
    特效 css3 渐变背景框
  • 原文地址:https://www.cnblogs.com/bluesky0122/p/9170515.html
Copyright © 2011-2022 走看看