msgpack用法

来源:互联网 发布:centos如何安装git 编辑:程序博客网 时间:2024/06/06 18:51
using MsgPack;
using MsgPack.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;


namespace WindowsFormsApplication2
{
    public class HttpUtils
    {




        public static string DoPostParam(string url, IEnumerator<string> dem, bool allowdir = false)
        {
            List<byte> postData = new List<byte>();
            bool hasParam = false;


            System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
            while (dem.MoveNext())
            {
                string name = dem.Current;


                // 忽略参数名或参数值为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (!hasParam)
                    {
                        postData.Add(0x93);
                        postData.Add(0xaa);
                    }
                    else
                    {
                        postData.Add(0xa3);


                    }


                    postData.AddRange(ASCII.GetBytes(name));




                    hasParam = true;
                }
            }


            return doreqoutpost(allowdir, url, postData.ToArray());
        }




        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
           
                return true;
           
        }
        public static MessagePackObjectDictionary DoPostParam<T>(string url, T dem, bool allowdir = false)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);


            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            //  req.CookieContainer = new CookieContainer();
            //X509Certificate2 cer = new X509Certificate2("c:\\123\\1.cer");
            //req.ClientCertificates.Add(cer);
            var serializer = SerializationContext.Default.GetSerializer<T>();


            req.Method = "POST";
            // WebProxy wp = new WebProxy("121.40.195.200:808");
            req.Headers["Cache-Control"] = "no-cache";
            req.Headers["Pragma"] = "no-cache";
            req.Host = "193.168.1.64:55553";
            req.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
            req.KeepAlive = true;
            //req.Proxy = wp;
            req.AllowAutoRedirect = allowdir;
            req.UserAgent = "Java/1.8.0_45";
            string responseData = String.Empty;
            req.Method = "POST";
            req.ContentType = "binary/message-pack";
            //req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {


                serializer.Pack(reqStream, dem);
            }


            CookieCollection ckl = new CookieCollection();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();


            //    WebHeaderCollection responseHeaders = res.Headers;


            //if (keepContext)
            //{
            //    context.Cookies = res.Cookies;
            //    context.Referer = _requestUrl;
            //}


            using (Stream stream = res.GetResponseStream())
            {
                //using (StreamReader streamReader = new StreamReader(stream))
                //{
                //    string msg = streamReader.ReadToEnd();
               //var pkg=   Unpacker.Create(stream);
              // var serializerOut = SerializationContext.Default.GetSerializer<MessagePackObjectDictionary>();


               return MessagePackSerializer.UnpackMessagePackObject(stream).AsDictionary();
              //  }
            }


            return null;
        }
        public static string doreqoutpost(bool allowdir, string urla, byte[] postdata)
        {


            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urla);


            //  req.CookieContainer = new CookieContainer();








            req.Method = "POST";
            // WebProxy wp = new WebProxy("121.40.195.200:808");
            req.Headers["Cache-Control"] = "no-cache";
            req.Headers["Pragma"] = "no-cache";
            req.Host = "193.168.1.64:55553";
            req.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
            req.KeepAlive = true;
            //req.Proxy = wp;
            req.AllowAutoRedirect = allowdir;
            req.UserAgent = "Java/1.8.0_45";
            string responseData = String.Empty;
            req.Method = "POST";
            req.ContentType = "binary/message-pack";
            //req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {


                BinaryWriter bw = new BinaryWriter(reqStream);
                bw.Write(postdata);
            }


            CookieCollection ckl = new CookieCollection();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();


            //    WebHeaderCollection responseHeaders = res.Headers;


            //if (keepContext)
            //{
            //    context.Cookies = res.Cookies;
            //    context.Referer = _requestUrl;
            //}


            using (Stream stream = res.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(stream))
                {
                    string msg = streamReader.ReadToEnd();


                    return msg;
                }
            }


            return "";




        }
        public static string doreqoutpost(bool allowdir, string urla, string postdata)
        {


            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(urla);


            //  req.CookieContainer = new CookieContainer();








            req.Method = "POST";
            // WebProxy wp = new WebProxy("121.40.195.200:808");


            //req.Proxy = wp;
            req.AllowAutoRedirect = allowdir;
            byte[] bs = Encoding.ASCII.GetBytes(postdata);
            string responseData = String.Empty;
            req.Method = "POST";
            req.ContentType = "binary/message-pack";
            //req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {


                BinaryWriter bw = new BinaryWriter(reqStream);
                bw.Write(postdata);
            }


            CookieCollection ckl = new CookieCollection();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();


            //    WebHeaderCollection responseHeaders = res.Headers;


            //if (keepContext)
            //{
            //    context.Cookies = res.Cookies;
            //    context.Referer = _requestUrl;
            //}


            using (Stream stream = res.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(stream))
                {
                    string msg = streamReader.ReadToEnd();


                    return msg;
                }
            }


            return "";




        }
    }
}
0 0