zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    ABP分层架构
    Quartz.Net任务统一调度框架
    Repository模式
    配置文件
    AspNet.WebAPI.OData.ODataPQ
    log4j+logback+slf4j+commons-logging的关系与调试(转)
    Log4jdbc demo
    如何有效地记录 Java SQL 日志(转)
    不要说自己没什么兴趣,也不要说自己兴趣很广泛,最好能具体到哪个方面,顺便提一下你最近看的一本该方面的书,有什么收获(转)
    前端是Sencha Touch+ Cordova(转)
  • 原文地址:https://www.cnblogs.com/lichmama/p/5710674.html
Copyright © 2011-2022 走看看