c# post时urlencode失效

/// <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失效

上一篇:《刻意练习之C#》-0018- C# 类的静态成员和实例成员


下一篇:ArchLinux——使用WINE-TIM头像异常解决办法