コード例 #1
0
        public void SetMethodName_BuildFor_MethodNameEquals()
        {
            var method  = HttpMethod.Get;
            var request = HttpClientRequest.Create(method);

            Assert.AreEqual(method.Name, request.Method.Name);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: eugeny-trunin/TinyClient
        static void Main(string[] args)
        {
            //Simple way:
            var client   = new HttpClient("http://myHost.io");
            var received = client.PostAndReceiveJson <MyAnswerVm>(
                query: "getMyAnswer",
                jsonSerializeableContent:  new MyRequestVM {
                Name = "Bender"
            });

            Console.WriteLine(received.Name);
            //need no to close connection

            //Fluent way:
            var answer = HttpClient
                         .Create("localhost:8080")
                         //Create the client
                         .Build()
                         //Send and receive
                         .SendJsonPost("users", new MyRequestVM {
                Name = "Bender"
            })
                         //Throw if status code not in [200-299]
                         .ThrowIfFailed()
                         //Cast answer from json to MyAnswerVm or throw
                         .GetJsonObject <MyAnswerVm>();

            Console.WriteLine(answer.Name);

            //hardcore way:
            var customClient = HttpClient
                               .Create("http://myHost.io")
                               .WithKeepAlive(true)
                               .WithCustomDecoder(ClientEncoders.Deflate)
                               .WithRequestTimeout(TimeSpan.FromSeconds(1))
                               .WithRequestMiddleware((r) => r.AddCustomHeader("sentBy", "MasterOfHardcore"))
                               .WithResponseMiddleware((r) => {
                if (r.StatusCode != HttpStatusCode.OK)
                {
                    throw new FormatException("Request failed with error: " + r.StatusCode);
                }
            })
                               .Build();

            //request uri is http://myHost.io/search?text=What+up&attributes=all
            var customRequest = HttpClientRequest
                                .Create(HttpMethod.Post, "/search")
                                .AddUriParam("text", "What up")
                                .AddUriParam("attributes", "all")
                                .AddCustomHeader("nugetPackage", "tinyClient")
                                .AddCustomHeader("_SessionId", "42")
                                .AddContentEncoder(ClientEncoders.Deflate)
                                .SetKeepAlive(true)
                                .SetTimeout(TimeSpan.FromSeconds(5))
                                .SetContent(new JsonContent(new MyRequestVM {
                Name = "Cartman"
            }));

            var textResponse = customClient
                               .Send(customRequest) as HttpResponse <string>;

            Console.WriteLine(textResponse?.Content);
        }