コード例 #1
0
        /// <summary>
        /// Api 调用。
        /// </summary>
        /// <typeparam name="T">Api 返回的结果类型。</typeparam>
        /// <param name="url">Api 地址。</param>
        /// <param name="method">要使用的 Http 方法。</param>
        /// <param name="postData">如果是 Post 表示要提交的 Api 数据。</param>
        /// <returns>Api 返回的结果对象。</returns>
        public static T ApiInvokeResult <T>(string url, HttpMethod method, string postData = null)
            where T : ResultModelBase
        {
            #region 参数校验

            if (string.IsNullOrEmpty(url))
            {
                throw new StringNullOrEmptyException(nameof(url));
            }

            #endregion

            string responseResultString = string.Empty;

            HttpReqeustClient client = new HttpReqeustClient();
            if (method == HttpMethod.Get)
            {
                responseResultString = client.HttpGetString(url);
            }
            else if (method == HttpMethod.Post)
            {
                responseResultString = client.HttpPost(url, new StringContent(postData));;
            }
            WeixinApp.Logger.Debug($"ApiInvokeResult Url:{url} Method:{method} Request:{postData} Response:{responseResultString}");

            T result = responseResultString.ToJsonObject <T>();
            result.ResponseResultString = responseResultString;
            return(result);
        }
コード例 #2
0
        public void HttpGetStringHasParaTest()
        {
            HttpReqeustClient client = new HttpReqeustClient();
            string            result = client.HttpGetString("http://localhost:5000/api/apitest/get/1");

            Assert.NotNull(result);
            Assert.True(result.Length > 0);
            Assert.Equal <string>("1", result);
        }
コード例 #3
0
        public void HttpSetHeadersTest()
        {
            HttpReqeustClient client = new HttpReqeustClient();

            client.Headers.Add("User-Agent", "HttpReqeustClient");

            string result = client.HttpGetString("http://localhost:5000/api/apitest/GetHttpHeaders");

            Assert.NotNull(result);
            Assert.True(result.Length > 0);
            Assert.True(result.Contains("HttpReqeustClient"));
        }
コード例 #4
0
        public void HttpGetStringTest()
        {
            HttpReqeustClient client = new HttpReqeustClient();
            string            result = client.HttpGetString("http://localhost:5000/api/apitest/get");

            Assert.NotNull(result);
            Assert.True(result.Length > 0);

            string[] resultObj = result.ToJsonObject <string[]>();
            Assert.NotNull(resultObj);
            Assert.Equal <string>("value1", resultObj[0]);
        }