提交post

#region XML方式提交
        public static void XML() {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
            wReq.Method = "POST";
            wReq.ContentType = "text/xml";
            wReq.Headers.Add("charset:utf-8");
            var encoding = Encoding.GetEncoding("utf-8");

if (GetXml() != null)
            {
                byte[] buffer = encoding.GetBytes(GetXml());
                wReq.ContentLength = buffer.Length;
                wReq.GetRequestStream().Write(buffer, 0, buffer.Length);
            }
            else {
                wReq.ContentLength = 0;
            }
        }
        /// <summary>
        /// 发送的XML
        /// </summary>
        /// <returns></returns>
        public static string GetXml() {
            StringBuilder str = new StringBuilder();
            str.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            str.Append("<Product>");
            str.Append("<Id>456</Id>");
            str.Append("<Name>ASDD</Name>");
            str.Append("<Categroy>QWER</Categroy>");
            str.Append("<Price>456</Price>");
            str.Append("</Product>");
            return str.ToString();
        }
        #endregion

#region Text提交方法
        public static void TEXT() {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
            wReq.Method = "POST";
            wReq.ContentType = "text/plain";

byte[] data = Encoding.Default.GetBytes("Id:798,Name:\"QW\",Categroy:\"ajsdkf\",Price:789");
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                string result = sr.ReadToEnd();
            }
        }
        #endregion
        #region JSON发送方法
        /// <summary>
        /// JSON发送方法
        /// </summary>
        public static void Json() {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
            wReq.Method = "POST";
            wReq.ContentType = "application/JSON";

byte[] data = Encoding.Default.GetBytes("{Id:123,Name:\"zwy\",Categroy:\"ajsdkf\",Price:123}");
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                string result = sr.ReadToEnd();
            }
        }
        #endregion
        #region Form提交

public static void Froms()
        {
            HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:30447/api/product/showname");
            wReq.Method = "POST";
            wReq.ContentType = "application/x-www-form-urlencoded";

string str = "Id:123,Name:\"zwy\",Categroy:\"ajsdkf\",Price:123";

byte[] data = Encoding.Default.GetBytes(str);
            wReq.ContentLength = data.Length;
            Stream reqStream = wReq.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();
            using (StreamReader sr = new StreamReader(wReq.GetResponse().GetResponseStream()))
            {
                string result = sr.ReadToEnd();
            }
        }

#endregion

获取

[HttpPost]
        public Product ShowName()
        {
            var prod=new Product();

var s = System.Web.HttpContext.Current.Request.InputStream;
            var b = new byte[s.Length];
            s.Read(b, 0, (int)s.Length);
            var str = Encoding.UTF8.GetString(b);
            try
            {
                //如果不是JSON报错
                var serializer = new JavaScriptSerializer();
                dynamic obj = serializer.Deserialize(str, typeof(object));
                //prod = serializer.Deserialize<Product>(str);

}
            catch (Exception ex)
            {
                try
                {
                    //如果不是xml,也不是json
                    var d = new XmlDocument();
                    d.LoadXml(str);
                    //prod=  DeserializeToObject<Product>(str);
                }
                catch (Exception e)
                {
                    //text文本
                    string index = str;
                }
     
            }
            return prod;
        }

最新文章

  1. SQL-日期函数
  2. Node学习笔记(二):事件驱动
  3. PHP curl 函数
  4. git --- push到远端
  5. case when then else end
  6. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题
  7. 二、CSS 基本介绍
  8. WCF入门教程(三)属性标签
  9. SQL Server - 聚集索引 &lt;第六篇&gt;
  10. 宝更容易使用比读IC卡信息的工具
  11. Eclipse中Lombok的安装和注解说明
  12. JUnit测试提示Java.lang.Exception: No runnable methods
  13. [宏]preempt_disable
  14. Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局
  15. 转:Linux实时将所有输出重定向到文件
  16. 惭愧, eclipse 之 build path
  17. InnoDB Master Thread I/O Rate详解
  18. [转载]从B 树、B+ 树、B* 树谈到R 树
  19. Sub-process /usr/bin/dpkg returned an error code (1)解决方法
  20. .net图表之ECharts随笔08-bar柱状图

热门文章

  1. #10017 传送带(SCOI 2010)(三分套三分)
  2. 总结const、readonly、static三者的区别【收藏、转载】20190614
  3. React-请求篇
  4. PyQt5+qtdesigner开发环境配置
  5. Codeforces 1192B 全dfs序 + 线段树
  6. Linux 普通用户自动修改密码
  7. 第五周作业—N42-虚怀若谷
  8. u盘被占用,无法弹出解决办法
  9. 顺序表 C++ 类模板实现
  10. 基于canvas实现的高性能、跨平台的股票图表库--clchart