c#访问webapi以及获取

提交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;
        }

posted @ 2018-02-02 16:13  行走在0和1之间  阅读(376)  评论(0编辑  收藏  举报