C# POST

参数拼接方法:&  类似url参数。然后转化为字节型

       string postdate =
                "Submit=" + Submit +
                "&dopost=" + dopost +
                "&position=" + position +
                "&saveset=" + saveset +
                "&showmod=" + showmod +
                "&templet=" + templet;

通用的post方法,参数会被转化为字节型(byte[])才能在post中传输

 /// <summary>
        /// send_发送POST请求
        /// </summary>
        /// <param name="vServerPage"></param>
        /// <param name="vPostData"></param>
        /// <returns></returns>
        public static string send_发送POST请求(string vServerPage, string vPostData)
        #region
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(vPostData);
            //创建请求
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(vServerPage);
            request.Method = "POST";
            request.ContentLength = dataArray.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            request.Timeout = 5000;
            //创建输入流
            Stream dataStream = null;
            try
            {
                dataStream = request.GetRequestStream();
            }
            catch (Exception)
            {
                return null;//连接服务器失败
            }

            //发送请求
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            //读取返回消息
            string res = string.Empty;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                res = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception ex)
            {
                return null;//连接服务器失败
            }
            return res;
        }

        #endregion
时间: 2024-11-06 09:32:40