• [C#] 后端post的请求方法


    C# 模拟post请求方法

    方法1:

    /// <summary>
            /// 模拟Post请求
            /// </summary>
            /// <param name="url">地址</param>
            /// <param name="postDataStr">参数:a=123&b=www</param>
            /// <returns></returns>
            public string HttpPost(string url, string postDataStr)
            {
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                byte[] arrB = encode.GetBytes(postDataStr);
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                myReq.Method = "POST";
                myReq.ContentType = "application/x-www-form-urlencoded";
                myReq.ContentLength = arrB.Length;
                Stream outStream = myReq.GetRequestStream();
                outStream.Write(arrB, 0, arrB.Length);
                outStream.Close();
    
    
                //接收HTTP做出的响应
                WebResponse myResp = myReq.GetResponse();
                Stream ReceiveStream = myResp.GetResponseStream();
                StreamReader readStream = new StreamReader(ReceiveStream, encode);
                Char[] read = new Char[256];
                int count = readStream.Read(read, 0, 256);
                string str = null;
                while (count > 0)
                {
                    str += new String(read, 0, count);
                    count = readStream.Read(read, 0, 256);
                }
                readStream.Close();
                myResp.Close();
    
                return str;
            }
    

    方法2:

    /// <summary>
        /// 发送POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public static string HttpPost(string url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
    
            return retString;
        }
    

      

  • 相关阅读:
    yii---模型的创建
    yii---控制器的创建
    yii的安装
    windows下安装composer
    wpgcms---列表页数据渲染
    Twig---基本使用
    wpgcms---详情页面数据怎么渲染
    Twig---的使用
    vue---指令怎么写
    vue---设置缩进为4个空格
  • 原文地址:https://www.cnblogs.com/shengwei/p/5527394.html
Copyright © 2020-2023  润新知