zoukankan      html  css  js  c++  java
  • 工具类

    /// <summary>
            /// post方法
            /// </summary>
            /// <param name="serverUrl"></param>
            /// <param name="reqJson"></param>
            /// <returns></returns>
            public static string postJson(string serverUrl, string reqJson)
            {
                var http = (HttpWebRequest)WebRequest.Create(new Uri(serverUrl));
                http.Accept = "application/json";
                http.ContentType = "application/json";
                http.Method = "POST";
    
                ASCIIEncoding encoding = new ASCIIEncoding();
                Byte[] bytes = encoding.GetBytes(reqJson);
    
                Stream newStream = http.GetRequestStream();
                newStream.Write(bytes, 0, bytes.Length);
                newStream.Close();
    
                var response = http.GetResponse();
    
                var stream = response.GetResponseStream();
                var sr = new StreamReader(stream);
                var content = sr.ReadToEnd();
    
                return content;
            }
    using Newtonsoft.Json;
    using System;
    using System.Reflection;
    
    namespace PayInterface
    {
        /// <summary>
        /// 工具类
        /// </summary>
        public class CommUtil
        {
            /// <summary>
            /// 根据Json字符串转换成实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="jsonstr"></param>
            /// <returns></returns>
            public static T ByJsonstrToModel<T>(string jsonstr)
            {
                T model = JsonConvert.DeserializeObject<T>(jsonstr);
                return model;
            }
    
            /// <summary>
            /// 将string转换为Decimal类型
            /// </summary>
            /// <param name="money"></param>
            /// <returns></returns>
            public static decimal MyToDecimal(string money)
            {
                decimal result = 0;
                decimal.TryParse(money, out result);
                return result;
            }
            /// <summary>
            /// 根据键值对为实际的类赋值并返回
            /// </summary>
            /// <typeparam name="T">T实体</typeparam>
            /// <param name="sParaTemp">键值对</param>
            /// <returns>T实体</returns>
            public static T SetModel<T>(System.Collections.Generic.SortedDictionary<string, string> sParaTemp)
            {  
                T obj = Activator.CreateInstance<T>();
                Type type = typeof(T);
                PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                string pName;
                bool exis = false;
                string _val;
                foreach (var item in props)
                {
                    pName = item.Name;
                    exis = sParaTemp.ContainsKey(pName);
                    if (exis)
                    {
                        //检测一下,属性是否存在。如果存在,才继续赋值。不然会出错。
                        if (obj.GetType().GetProperty(pName) != null)
                        {
                            _val = sParaTemp[pName];
                            //主要是使用了Convert.ChangeType来实现。
                            //但是如果bonus在xml的值是1,就会出错。好像转不到bool型。所以,在xml里,我强制了要写false和true。
                            obj.GetType().GetProperty(pName).SetValue(obj, Convert.ChangeType(_val, obj.GetType().GetProperty(pName).PropertyType), null);
                        }
                    }
                }
                return obj;
            }
    
    
            /// <summary>
            /// 根据键值对为实际的类赋值并返回
            /// </summary>
            /// <typeparam name="T">T实体</typeparam>
            /// <param name="sParaTemp">键值对</param>
            /// <returns>T实体</returns>
            public static T SetModel<T>(System.Collections.Generic.SortedDictionary<string, object> sParaTemp)
            {
                T obj = Activator.CreateInstance<T>();
                Type type = typeof(T);
                PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                string pName;
                bool exis = false;
                string _val;
                foreach (var item in props)
                {
                    pName = item.Name;
                    exis = sParaTemp.ContainsKey(pName);
                    if (exis)
                    {
                        //检测一下,属性是否存在。如果存在,才继续赋值。不然会出错。
                        if (obj.GetType().GetProperty(pName) != null)
                        {
                            _val = sParaTemp[pName].ToString();
                            //主要是使用了Convert.ChangeType来实现。
                            //但是如果bonus在xml的值是1,就会出错。好像转不到bool型。所以,在xml里,我强制了要写false和true。
                            obj.GetType().GetProperty(pName).SetValue(obj, Convert.ChangeType(_val, obj.GetType().GetProperty(pName).PropertyType), null);
                        }
                    }
                }
                return obj;
            }
    
            /// <summary>
            /// 获取请求端的IP
            /// </summary>
            /// <returns></returns>
            public static string GetRequestIp(System.Web.HttpRequest Req)
            {
    
                string loginip = string.Empty;
                //Request.ServerVariables[""]--获取服务变量集合   
                if (Req.ServerVariables["REMOTE_ADDR"] != null) //判断发出请求的远程主机的ip地址是否为空  
                {
                    //获取发出请求的远程主机的Ip地址  
                    loginip = Req.ServerVariables["REMOTE_ADDR"].ToString();
                }
                //判断登记用户是否使用设置代理  
                else if (Req.ServerVariables["HTTP_VIA"] != null)
                {
                    if (Req.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                    {
                        //获取代理的服务器Ip地址  
                        loginip = Req.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                    }
                    else
                    {
                        //获取客户端IP  
                        loginip = Req.UserHostAddress;
                    }
                }
                else
                {
                    //获取客户端IP  
                    loginip = Req.UserHostAddress;
                }
                return loginip;
            }
        }
    }
  • 相关阅读:
    Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation
    Chapter 6 -- Caches
    SVN OPS发布总结
    Chapter 5 -- ImmutableCollections
    差点掉坑,MySQL一致性读原来是有条件的
    PHP实现的一个时间帮助类
    H5拍照、选择图片上传组件核心
    Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化
    javascript-函数表达式
    javascript遍历方法总结
  • 原文地址:https://www.cnblogs.com/daixingqing/p/4806220.html
Copyright © 2011-2022 走看看