C# 实现Get, Post方法

C# 实现Get, Post方法
        /// <summary>
        /// Get 数据
        /// </summary>
        /// <param name="url">目标Url</param>
        /// <returns></returns>
        public static string GetData(string url)
        {
            try
            {
                string result = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.Method = "Get";
                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

                using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    result = sr.ReadToEnd();
                }
                httpWebResponse.Close();
                httpWebRequest.Abort();
                return result;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Post 数据
        /// </summary>
        /// <param name="url">目标Url</param>
        /// <returns></returns>
        public static string PostData(string url)
        {
            try
            {
                string result = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";
                //httpWebRequest.Timeout = 20000;

                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    result = sr.ReadToEnd();
                }
                httpWebResponse.Close();
                httpWebRequest.Abort();
                return result;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Post 数据
        /// </summary>
        /// <param name="url">目标Url</param>
        /// <param name="data">发送数据</param>
        /// <returns></returns>
        public static string PostData(string url, string data)
        {
            try
            {
                string result = "";
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";
                //httpWebRequest.Timeout = 20000;

                // byte[] buffer = Encoding.Default.GetBytes(HttpUtility.UrlEncode("username=test1&password=123456"));  
                // byte[] buffer = Encoding.UTF8.GetBytes("{\"username\":\"test1\",\"password\":\"123456\"}");
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                httpWebRequest.ContentLength = buffer.Length;
                Stream requestStream = httpWebRequest.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);
                requestStream.Close();

                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
                httpWebResponse.Close();
                httpWebRequest.Abort();
                return result;
            }
            catch
            {
                throw;
            }
        }

作者最新文章
Winform TextBox (文本框) 密码输入框, 输入内容显示*号 (星号)
Winform 启动前弹出登录界面, 登录成功之后再显示主界面
C# 字符串string和字符数组char[]的相互转换
C# 字符串倒序输出, 实现字符串倒序的方法
Winform 如何从 TextBox(文本框) 控件读取每一行数据到数组