post formdata

 
//////////////////////////////////////////////////////////////////////////
    var url = "XXX"; 

            var cellphone = "xxx";
            var msg = "{\"identys\":[\"" + cellphone + "\"],\"title\":\通知\",\"msg\":\"测试一下\"}";
            var msgbt = System.Text.Encoding.UTF8.GetBytes(msg);

            var msg64 = Convert.ToBase64String(msgbt); 

            PostUtils post = new PostUtils();

            Dictionary<string, string> formdata = new Dictionary<string, string>();

            formdata.Add("queuename", "ydztMsg");
            formdata.Add("msg", msg64);

            post.PostFormData(url, formdata, (r, m) => {

                Console.WriteLine($"{r}---{m}");
            });

////////////////////////////////////////////////////////////////////



 public class PostUtils
    {


        public PostUtils()
        {

        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="formDatas"></param>
        /// <param name="PostComplete">请求完成</param>
        public void PostFormData(string url,Dictionary<string,string> formDatas, Action<bool,string> PostComplete)
        {

            var result = true;
            var msg = "";
            try
            {
                byte[] buffer = new byte[1024 * 1000];//缓冲区  

                var request = WebRequest.Create(url) as HttpWebRequest;

                request.Method = "POST";

                var boundary = "----" + DateTime.Now.Ticks.ToString("x");

                request.ContentType = "multipart/form-data; boundary=----" + boundary;

                string msgreq = string.Empty;

                foreach (KeyValuePair<string, string> kv in formDatas)
                {
                    msgreq += $"------{ boundary}\r\nContent-Disposition:  form-data; name =\"{kv.Key}\"\r\n\r\n {kv.Value} \r\n";

                }
                msgreq += $"\r\n------{ boundary }----";

                var data = Encoding.UTF8.GetBytes(msgreq);

                request.ContentLength = data.Length;

                using (var writer = request.GetRequestStream())
                {
                    writer.Write(data, 0, data.Length);
                }  
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                        { 
                            msg = reader.ReadToEnd();
                        }
                    }
                }
            }
            
            catch(Exception e)
            {
                result = false;
                msg = e.Message;
            }

            finally
            {
                PostComplete?.Invoke(result, msg);
            }
        }

    }

 from-data格式太麻烦了。

上一篇:LeetCode 1020. Number of Enclaves


下一篇:算法排序————快速排序,堆排序,归并排序