码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-04-30 22:34:39      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:c#   模拟post提交   httpwebrequest   

/// <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形式传递)

标签:c#   模拟post提交   httpwebrequest   

原文地址:http://blog.csdn.net/chinaplan/article/details/24720457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!