예제 #1
0
        public void Test_HttpClient()
        {
            IEasyHttpClient hc   = new DefaultEasyHttpClient();
            bool            done = false;

            Task.Run(async() =>
            {
                try
                {
                    //var res = hc.SetBaseUrl("http://localhost:8090").Post("/api/categorys", new { id = "1" }.ToJson());
                    //Debug.WriteLine(res.Result);


                    //var res1 = hc.SetBaseUrl("http://localhost:8090").Get("/api/category/1").ContinueWith((r)=> {
                    //    if (r.IsCanceled)
                    //    {
                    //        Debug.WriteLine("Canceled");
                    //    }
                    //    if (r.IsCompletedSuccessfully)
                    //    {
                    //        Debug.WriteLine(r.Result);
                    //    }
                    //});

                    var res3 = hc.SetBaseUrl("http://192.168.21.197").GetStream("/download1/fdat").ContinueWith((s) => {
                        s.Result.Position = 0;
                        using (FileStream fs = new FileStream("fdat", FileMode.OpenOrCreate))
                        {
                            s.Result.CopyToAsync(fs);
                            fs.Flush();
                        }
                    });



                    done = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            });
            while (!done)
            {
                Thread.Sleep(3000);
            }
            Console.WriteLine(done);
        }
예제 #2
0
        /// <summary>
        /// 呼叫api
        /// </summary>
        /// <typeparam name="TReq"></typeparam>
        /// <typeparam name="TRes"></typeparam>
        /// <param name="funcUrl"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        public TRes CallApi <TReq, TRes>(string funcUrl, TReq req)
        {
            string resStr = string.Empty;

            var fullUrl = ServiceUrlMaker.MakerUrl(this, funcUrl);
            var reqjson = req.ToJson();
            var logstr  = $"URL: {fullUrl}, REQUEST: {reqjson}";

            Log(logstr);
            var t = Task.Run(async() => {
                IEasyHttpClient hc     = new DefaultEasyHttpClient();
                hc.MakeExceptionResult = (ex) =>
                {
                    var err_res = DataShellCreator.CreateFail <TRes>(ex);
                    return(err_res.ToJson());
                };
                resStr = await hc.SetBaseUrl(this.ServiceUrlMaker.BaseUrl).Post(funcUrl, reqjson);
            });

            Task.WaitAll(t);
            Log($"接口桥返回数据:{resStr}");
            return(ResTrans <TRes>(resStr));
        }