c# winform 使用webapi

public static string HttpPostNoFile(string url, Dictionary<string,string> para)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.Method = "POST";//get或者post  

            StringBuilder buffer = new StringBuilder();//这是要提交的数据
            int i = 0;
            //通过泛型集合转成要提交的参数和数据
            foreach (string key in para.Keys)
            {
                if (i > 0)
                {
                    buffer.AppendFormat("&{0}={1}", key, para[key]);
                }
                else
                {
                    buffer.AppendFormat("{0}={1}", key, para[key]);
                }
                i++;
            }

            byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString());//UTF-8
            request.ContentLength = bs.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd().ToString();
                }
                
            }
        }

        public static string HttpGet(string url, Dictionary<string, string> para)
        {
            Encoding encoding = Encoding.UTF8;
            WebClient webClient = new WebClient();
            StringBuilder buffer = new StringBuilder("?");//这是要提交的数据
            int i = 0;
            foreach (string key in para.Keys)
            {
                string pk = para[key].Replace('_', '-');
                if (i > 0)
                {
                    buffer.AppendFormat("&{0}={1}", key, pk);
                }
                else
                {
                    buffer.AppendFormat("{0}={1}", key, pk);
                }
                i++;
            }
            url = url + buffer.ToString();

            Stream stream = webClient.OpenRead(url);
            StreamReader sr = new StreamReader(stream);
            string str = sr.ReadToEnd();
            return str;
        }



        /// <summary>
        /// 把对象转换为JSON字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToJSON(this object o)
        {
            if(o == null)
            {
                return null;
            }

            return JsonConvert.SerializeObject(o);
        }

        /// <summary>
        /// 把Json文本转为实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static T FromJSON<T>(this string input)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(input);
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

  

posted @ 2019-10-29 09:31  HongEgg  阅读(2744)  评论(0编辑  收藏  举报