zoukankan      html  css  js  c++  java
  • [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;
        }
    

      

  • 相关阅读:
    二叉搜索树与双向链表
    TCP 三次握手与四次挥手
    复杂链表的复制
    二叉树中和为某一值的路径
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
    栈的压入、弹出序列
    jenkins 持续集成和交付——一个构件小栗子前置(三)
    jenkins 持续集成和交付——gogs安装(外篇)
    jenkins 持续集成和交付——安装与账户安全还有凭证(二)
  • 原文地址:https://www.cnblogs.com/shengwei/p/5527394.html
Copyright © 2011-2022 走看看