C#转发Post请求,包括参数和文件

        /// <summary>
        /// 转发Post请求
        /// </summary>
        /// <param name="curRequest">要转发的请求</param>
        /// <param name="url">转发到的Url地址</param>
        public static string ForwardRequest(HttpRequest curRequest, string url)
        {
            byte[] inputBytes;
            int inputStreamLength;
            using (var inputStream = curRequest.InputStream)
            {
                inputStreamLength = Convert.ToInt32(inputStream.Length);
                inputBytes = new byte[inputStreamLength + 1];
                inputStream.Read(inputBytes, 0, inputStreamLength);
                inputStream.Close();
            }

            //构造请求
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = curRequest.ContentType;
            request.ContentLength = curRequest.ContentLength;
            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(inputBytes, 0, inputStreamLength);
                requestStream.Close();
            }

            string result = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                if (response != null)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
            }

            return result;
        }
原文地址:https://www.cnblogs.com/feigao/p/9647238.html