C#工作笔记

没想到一个Java后端开发还要负责C#桌面程序,我感觉有点方。不过方归方,活还是要干的。简单记录下学到的一些知识点。

 

1.引用API函数

namespace Demo
{
    class MyUtil
    {
        [DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(
            byte bVk,       //虚拟键值  
            byte bScan,     // 一般为0  
            int dwFlags,    //这里是整数类型 0 为按下,2为释放  
            int dwExtraInfo //这里是整数类型 一般情况下设成为0  
        ); 
        
        public static void sendF9()
        {
            keybd_event((byte)Keys.F9, 0, 0, 0);
            keybd_event((byte)Keys.F9, 0, 2, 0);
        }
    }
}

 

2.对象序列化,因为要对接Java的rest接口,所以需要将对象序列化转json

using System.Runtime.Serialization;

namespace Demo
{
    [DataContract]
    class ServiceError
    {
        [DataMember]
        public string errCode { get; set; }

        [DataMember]
        public string errDesc { get; set; }
    }
}
        /// <summary>
        /// 将对象转化为JSON字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream stream = new MemoryStream();
            serializer.WriteObject(stream, obj);
            byte[] dataBytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(dataBytes, 0, (int)stream.Length);
            return Encoding.UTF8.GetString(dataBytes);
        }
        /// <summary>
        /// 将Json字符串转化成对象
        /// </summary>
        /// <typeparam name="T">转换的对象类型</typeparam>
        /// <param name="output">json字符串</param>
        /// <returns></returns>
        public static T ToClass<T>(string output)
        {
            object result = null;
            DataContractJsonSerializer outDs = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream outMs = new MemoryStream(Encoding.UTF8.GetBytes(output)))
            {
                try
                {
                    result = outDs.ReadObject(outMs);
                }
                catch (SerializationException ex)
                {
                    Console.WriteLine(ex.TargetSite + ": " + ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
            }
            return (T)result;
        }

 

3.发送POST请求

        /// <summary>  
        /// POST请求与获取结果  
        /// </summary>  
        public static string HttpPost(string Url, string postDataStr)
        {
            Console.WriteLine("HttpPost: postDataStr = " + postDataStr);
            string retVal = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/json";
                StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
                writer.Write(postDataStr);
                writer.Flush();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                retVal = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.TargetSite + ": " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.WriteLine("HttpPost: retVal = " + retVal);
            return retVal;
        }

 

posted @ 2016-07-27 12:40  lichmama  阅读(293)  评论(0编辑  收藏  举报