C# 实现Get, Post方法

发布时间:2020-08-06 16:08 作者:独孤剑 阅读:324

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


微信打赏, 微信扫一扫

支付宝打赏, 支付宝扫一扫

如果文章对您有帮助,欢迎给作者打赏

作者最新文章
拜登称政府正调查燃油公司黑客攻击事件 确定该公司网络被勒索软件感染
云南大理森林消防直升机坠入洱海:4名机组人员不幸遇难
jquery $.ajax()方法参数详解
java 使用 itext 导出 PDF 文本绝对定位, itext 绝对定位的方法
杭州野生动物世界“金钱豹出逃”发布致歉声明
企业信息查询系统