参数请求post, get , delete中的基本使用(2)

UTF-8数字编码

/// <summary>
        /// 参数的Url请求
        /// </summary>
        /// <returns></returns>
        public static string Anlv_RequestPostService(string PostData, string Url)
        {
            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url);
                myReq.AllowAutoRedirect = false;
                myReq.Method = "POST";
                myReq.ContentType = "application/x-www-form-urlencoded";
                myReq.KeepAlive = true;

                //把数组转换成流中所需字节数组类型
                Encoding code = Encoding.GetEncoding("UTF-8");
                byte[] bytesRequestData = code.GetBytes(PostData);

                //填充POST数据
                myReq.ContentLength = bytesRequestData.Length;
                Stream requestStream = myReq.GetRequestStream();
                requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                requestStream.Close();

                //发送POST数据请求服务器

                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream();

                //获取服务器返回信息


                StreamReader reader = new StreamReader(myStream, code);
                StringBuilder responseData = new StringBuilder();
                String line;
                while ((line = reader.ReadLine()) != null)
                {
                    responseData.Append(line);
                }
                //释放
                myStream.Close();

                return responseData.ToString();
            }
            catch (Exception ex) { return ""; }
        }
View Code

GBK数字编码

/// <summary>
        /// 参数的Url请求
        /// </summary>
        /// <returns></returns>
        public static string RequestPostService(string PostData,string Url)
        {
            try
            {
                string XMLPostData="xml=" + PostData+ "";

                //设置HttpWebRequest基本信息
                HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(Url);
                myReq.AllowAutoRedirect = false;
                myReq.Method = "POST";
                myReq.ContentType = "application/x-www-form-urlencoded";
                myReq.KeepAlive = true;

                //把数组转换成流中所需字节数组类型
                Encoding code = Encoding.GetEncoding("GBK");
                byte[] bytesRequestData = code.GetBytes(XMLPostData);

                //填充POST数据
                myReq.ContentLength = bytesRequestData.Length;
                Stream requestStream = myReq.GetRequestStream();
                requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                requestStream.Close();

                //发送POST数据请求服务器

                HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
                Stream myStream = HttpWResp.GetResponseStream();

                //获取服务器返回信息

                StreamReader reader = new StreamReader(myStream, code);
                StringBuilder responseData = new StringBuilder();
                String line;
                while ((line = reader.ReadLine()) != null)
                {
                    responseData.Append(line);
                }
                //释放
                myStream.Close();

                return responseData.ToString();
            }
            catch (Exception ex) { return ""; }
        }
View Code
原文地址:https://www.cnblogs.com/ly77461/p/5708280.html