首页 > 代码库 > C# 模拟POST提交(根据URL地址,参数以xml形式传递)

C# 模拟POST提交(根据URL地址,参数以xml形式传递)

/// <summary>
        /// 模拟POST提交
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="uploadData">xml参数</param>
        /// <returns>返回结果</returns>
        public string PostJDWebHtml(String url, String uploadData)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            // Encode the data
            byte[] encodedBytes = Encoding.UTF8.GetBytes(uploadData);
            myHttpWebRequest.ContentLength = encodedBytes.Length;

            // Write encoded data into request stream
            Stream requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(encodedBytes, 0, encodedBytes.Length);
            requestStream.Close();

            HttpWebResponse result;

            try
            {
                result = (HttpWebResponse)myHttpWebRequest.GetResponse();
            }
            catch
            {
                return string.Empty;
            }

            if (result.StatusCode == HttpStatusCode.OK)
            {
                using (Stream mystream = result.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(mystream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }

            return null;

        }

C# 模拟POST提交(根据URL地址,参数以xml形式传递)