国产97色在线|亚洲-欧美人妻另类制服丝袜-欧美人成国产91视频-殷素素一女战二夫|www.ycjrc.net

C# 實現(xiàn)Get, Post方法

發(fā)布時間:2020-08-06 16:08 作者:獨孤劍 閱讀:2278

C# 實現(xiàn)Get, Post方法
        /// <summary>
        /// Get 數(shù)據(jù)
        /// </summary>
        /// <param name="url">目標(biāo)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 數(shù)據(jù)
        /// </summary>
        /// <param name="url">目標(biāo)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 數(shù)據(jù)
        /// </summary>
        /// <param name="url">目標(biāo)Url</param>
        /// <param name="data">發(fā)送數(shù)據(jù)</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;
            }
        }


微信打賞, 微信掃一掃

支付寶打賞, 支付寶掃一掃

如果文章對您有幫助,歡迎給作者打賞

作者最新文章
開發(fā)過程中解決360兼容模式瀏覽器的方法
云南象群向西南方向遷移,云南離群獨象距離象群約12公里
吉林做網(wǎng)站最低價格,吉林企業(yè)網(wǎng)站建設(shè)價格低至500元起
守象人直擊云南象群最新動向
網(wǎng)站影響百度蜘蛛抓取量的因素有哪些?為什么我的網(wǎng)站Baidu蜘蛛來的次數(shù)少?
企業(yè)名片
在線客服