Пример #1
0
        /// <summary>將 QueryString 資料以 WebAPI GET 傳輸方式傳送至遠端伺服器</summary>
        /// <param name="url">遠端伺服器網址</param>
        /// <param name="queryString">欲傳送的資料</param>
        /// <param name="timeout">逾時時間,單位豪秒</param>
        /// <returns>自遠端伺服器回傳的資料(或網頁內容)</returns>
        /// <exception cref="ArgumentNullException">url 參數為 null。</exception>
        /// <exception cref="ArgumentOutOfRangeException">逾時時間小於等於 0。</exception>
        /// <exception cref="WebException">傳輸錯誤</exception>
        public static string GetWebAPI(string url, NameValueCollection queryString, int timeout = 5000)
        {
            string result = null;

            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }
            if (timeout <= 0)
            {
                throw new ArgumentOutOfRangeException("timeout");
            }
            using (ExtWebClient wc = new ExtWebClient(timeout))
            {
                wc.Encoding = System.Text.Encoding.UTF8;
                if (queryString != null)
                {
                    wc.QueryString = queryString;
                }
                result = wc.DownloadString(url);
            }
            return(result);
        }