首页 > Windows开发 > 详细

c# post时urlencode失效

时间:2020-06-12 23:56:00      阅读:131      评论:0      收藏:0      [点我收藏+]
/// <summary>
        /// POST数据到指定地址
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string PostDataToUrl(string url, System.Text.Encoding encoding, System.Collections.Specialized.NameValueCollection data)
        {
            //设置HttpWebRequest
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(new Uri(url));
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.KeepAlive = false;

            byte[] buffer;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                for (int i = 0; i < data.Count; i++)
                {
                    buffer = encoding.GetBytes(data.Keys[i]);
                    if (i > 0)
                        ms.WriteByte(Convert.ToByte(&));
                    ms.Write(buffer, 0, buffer.Length);
                    ms.WriteByte(Convert.ToByte(=));
                    buffer = encoding.GetBytes(HttpUtility.UrlEncode(data[i]));
                    ms.Write(buffer, 0, buffer.Length);
                }
                buffer = ms.ToArray();
            }

            request.ContentLength = buffer.Length;
            using (
                
                
                System.IO.Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(buffer, 0, buffer.Length);
                requestStream.Close();
            }

            try
            {
                //获取数据
                using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)(request.GetResponse()))
                using (System.IO.Stream stream = response.GetResponseStream())
                using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, encoding))
                {
                    string result = reader.ReadToEnd();
                    reader.Close();
                    stream.Close();
                    response.Close();
                    return result;
                }
            }
            finally
            {
                request.Abort();
            }
        }
   调用方法:
      Byte[] bytes = Encoding.UTF8.GetBytes(msg);
            //转换成Base64字符串
            msg = BytesToBase64(bytes);
            string encodeMsgs = System.Web.HttpUtility.UrlEncode(msg);//urlencode编码
            NameValueCollection vc = new NameValueCollection();
            vc.Add("sid", sid);
            vc.Add("mobi", mobi);
            vc.Add("sign", sign);
            vc.Add("msg", encodeMsgs);

参考地址:https://www.cnblogs.com/MissQing/p/7205730.html

c# post时urlencode失效

原文:https://www.cnblogs.com/myLeisureTime/p/13110667.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!