コード例 #1
0
        /// <summary>
        /// 获取Bool类型的下拉框
        /// </summary>
        /// <param name="boolType"></param>
        /// <param name="defaultValue"></param>
        /// <param name="trueText"></param>
        /// <param name="falseText"></param>
        /// <param name="selectText"></param>
        /// <returns></returns>
        public static List <ComboSelectListItem> GetBoolCombo(BoolComboTypes boolType, bool?defaultValue = null, string trueText = null, string falseText = null, string selectText = null)
        {
            List <ComboSelectListItem> rv = new List <ComboSelectListItem>();
            string yesText = "";
            string noText  = "";

            switch (boolType)
            {
            case BoolComboTypes.YesNo:
                yesText = "是";
                noText  = "否";
                break;

            case BoolComboTypes.ValidInvalid:
                yesText = "有效";
                noText  = "无效";
                break;

            case BoolComboTypes.MaleFemale:
                yesText = "男";
                noText  = "女";
                break;

            case BoolComboTypes.HaveNotHave:
                yesText = "有";
                noText  = "无";
                break;

            case BoolComboTypes.Custom:
                yesText = trueText ?? "是";
                noText  = falseText ?? "否";
                break;

            default:
                break;
            }
            ComboSelectListItem yesItem = new ComboSelectListItem()
            {
                Text  = yesText,
                Value = "true"
            };

            if (defaultValue == true)
            {
                yesItem.Selected = true;
            }
            ComboSelectListItem noItem = new ComboSelectListItem()
            {
                Text  = noText,
                Value = "false"
            };

            if (defaultValue == false)
            {
                noItem.Selected = true;
            }
            rv.Add(yesItem);
            rv.Add(noItem);
            return(rv);
        }
コード例 #2
0
ファイル: APIHelper.cs プロジェクト: jacklmjie/dotnet-iRED
        /// <summary>
        /// 调用远程地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="method">请求方式</param>
        /// <param name="postdata">Post数据</param>
        /// <param name="timeout">超时秒数</param>
        /// <param name="proxy">代理</param>
        /// <returns>远程方法返回的内容</returns>
        public static async Task <string> CallAPI(string url, HttpMethodEnum method = HttpMethodEnum.GET, IDictionary <string, string> postdata = null, int?timeout = null, string proxy = null)
        {
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    return(string.Empty);
                }
                //新建http请求
                var request = WebRequest.Create(url) as HttpWebRequest;
                //如果配置了代理,则使用代理
                if (string.IsNullOrEmpty(proxy) == false)
                {
                    request.Proxy = new WebProxy(proxy);
                }

                request.Method = method.ToString();
                //如果是Post请求,则设置表单
                if (method == HttpMethodEnum.POST || method == HttpMethodEnum.PUT)
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                    if (postdata == null || postdata.Count == 0)
                    {
                        request.Headers[HttpRequestHeader.ContentLength] = "0";
                    }
                }
                request.Headers[HttpRequestHeader.UserAgent] = DefaultUserAgent;
                //设置超时
                if (timeout.HasValue)
                {
                    request.ContinueTimeout = timeout.Value;
                }
                request.CookieContainer = new CookieContainer();
                //填充表单数据
                if (!(postdata == null || postdata.Count == 0))
                {
                    var buffer = new StringBuilder();
                    var i      = 0;
                    foreach (string key in postdata.Keys)
                    {
                        if (i > 0)
                        {
                            buffer.AppendFormat("&{0}={1}", key, postdata[key]);
                        }
                        else
                        {
                            buffer.AppendFormat("{0}={1}", key, postdata[key]);
                        }
                        i++;
                    }
                    var data = Encoding.UTF8.GetBytes(buffer.ToString());
                    using (var stream = await request.GetRequestStreamAsync())
                    {
                        stream.Write(data, 0, data.Length);
                    }
                }


                var res = await request.GetResponseAsync();

                var    wsp = (HttpWebResponse)res;
                Stream st  = null;
                //如果远程服务器采用了gzip,增进行相应的解压缩
                if (wsp.Headers[HttpResponseHeader.ContentEncoding]?.ToLower().Contains("gzip") == true)
                {
                    st = new GZipStream(st, CompressionMode.Decompress);
                }
                else
                {
                    st = wsp.GetResponseStream();
                }
                //设置编码
                var encode = Encoding.UTF8;
                if (!string.IsNullOrEmpty(wsp.Headers[HttpResponseHeader.ContentEncoding]))
                {
                    encode = Encoding.GetEncoding(wsp.Headers[HttpResponseHeader.ContentEncoding]);
                }
                //读取内容
                var sr = new StreamReader(st, encode);
                var ss = sr.ReadToEnd();
                sr.Dispose();
                wsp.Dispose();
                return(ss);
            }
            catch
            {
                //返回失败信息
                ComboSelectListItem rv = new ComboSelectListItem()
                {
                    Text  = "连接失败",
                    Value = null
                };
                return(JsonSerialize(rv));
            }
        }