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;
            }
        }
        ///


        /// 发送的XML
        ///

        ///
        public static string GetXml() {
            StringBuilder str = new StringBuilder();
            str.Append("");
            str.Append("");
            str.Append("456");
            str.Append("ASDD");
            str.Append("QWER");
            str.Append("456");
            str.Append("
");
            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发送方法
        ///
        /// JSON发送方法
        ///

        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(str);

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

转载于:https://www.cnblogs.com/zwyAndDong/p/8405695.html

你可能感兴趣的:(c#访问webapi以及获取)