工具类

/// <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;
        }
    }
}

 

posted @ 2015-09-14 09:04  一千零一夜  阅读(175)  评论(0编辑  收藏  举报