当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.Send方法代码示例

本文整理汇总了C#中HttpClient.Send方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.Send方法的具体用法?C# HttpClient.Send怎么用?C# HttpClient.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpClient的用法示例。


在下文中一共展示了HttpClient.Send方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Given_that_http_reponse_is_not_valid_When_sending_Then_HttpInvalidResponseException_thrown

        public void Given_that_http_reponse_is_not_valid_When_sending_Then_HttpInvalidResponseException_thrown()
        {
            var mockResponse = "";

            var mockSocket = new Mock<IHttpSocket>();
            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var target = new HttpClient(mockSocket.Object);

            // execution/assertion
            TestDelegate testMethod = () => target.Send(new HttpRequest(""));
            Assert.Throws(typeof(HttpInvalidResponseException), testMethod);
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:15,代码来源:SendTest.cs

示例2: Given_url_and_port_and_arguments_are_set_When_sending_Then_request_is_sent_via_socket

        public void Given_url_and_port_and_arguments_are_set_When_sending_Then_request_is_sent_via_socket()
        {
            // setup
            var mockHostname = "www.test.com";
            var mockPath = "/resource.html";
            var mockUrl = mockHostname + mockPath;
            var mockPort = 8080;
            var mockSocket = new Mock<IHttpSocket>();

            var mockRequest = new HttpRequestBuilder()
                .SetUrl(mockUrl)
                .SetPort(mockPort)
                .Build();

            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(200)
                .SetResponse("response")
                .Build();

            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var target = new HttpClient(mockSocket.Object);

            // execution
            target.Send(mockRequest);

            // assertion
            mockSocket.Verify(
                s => s.Request(It.Is<HttpRequest>(
                    hr => hr.Hostname == mockHostname &&
                        hr.Path == mockPath &&
                        hr.Port == mockPort &&
                        hr.Method == HttpMethod.GET)),
                Times.Once(),
                "Request not sent via socket");
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:38,代码来源:SendTest.cs

示例3: Given_that_http_status_code_1xx_is_returned_When_sending_Then_HttpUnhandledStatusException_thrown

        public void Given_that_http_status_code_1xx_is_returned_When_sending_Then_HttpUnhandledStatusException_thrown()
        {
            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(100)
                .SetStatusDescriptor("Redirection")
                .SetResponse("Some response")
                .Build();

            var mockSocket = new Mock<IHttpSocket>();
            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var target = new HttpClient(mockSocket.Object);

            // execution/assertion
            TestDelegate testMethod = () => target.Send(new HttpRequest(""));
            var exception = Assert.Throws(typeof(HttpRequestException), testMethod);
            Assert.AreEqual("100 Redirection", exception.Message, "Exception message is not correct");
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:20,代码来源:SendTest.cs

示例4: HttpResponseBuilder

        public void Given_that_request_is_present_and_status_code_indicates_error_When_sending_Then_request_Success_is_set_false()
        {
            var content = "some response content";
            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(404)
                .SetStatusDescriptor("OK")
                .SetResponse(content)
                .Build();

            var mockSocket = new Mock<IHttpSocket>();
            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var mockRequest = new HttpRequest("");

            var target = new HttpClient(mockSocket.Object);

            // execution/assertion
            TestDelegate method = () => target.Send(mockRequest);
            var exception = (HttpRequestException)Assert.Throws(typeof (HttpRequestException), method,
                "Correct exception not thrown");

            Assert.IsFalse(exception.Request.Success, "The request success property is not correct");
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:25,代码来源:SendTest.cs

示例5: Given_that_request_is_present_When_sending_Then_request_status_code_is_set

        public void Given_that_request_is_present_When_sending_Then_request_status_code_is_set()
        {
            var content = "some response content";
            var statusCode = 201;
            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(statusCode)
                .SetStatusDescriptor("OK")
                .SetResponse(content)
                .Build();

            var mockSocket = new Mock<IHttpSocket>();
            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var mockRequest = new HttpRequest("");

            var target = new HttpClient(mockSocket.Object);

            // execution
            
            var result = target.Send(mockRequest);

            // assertion
            Assert.AreEqual(statusCode, mockRequest.StatusCode, "The request status code is not correct");
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:26,代码来源:SendTest.cs

示例6: Given_that_http_status_code_2xx_And_http_headers_are_returned_When_sending_Then_response_is_returned

        public void Given_that_http_status_code_2xx_And_http_headers_are_returned_When_sending_Then_response_is_returned()
        {
            var content = "some response content";
            var mockResponse = new HttpResponseBuilder()
                .SetStatusCode(200)
                .SetStatusDescriptor("OK")
                .SetResponse(content)
                .AddHeader("Header-one: header stuff")
                .AddHeader("Second-header: more header stuff")
                .Build();

            var mockSocket = new Mock<IHttpSocket>();
            mockSocket
                .Setup(s => s.Request(It.IsAny<HttpRequest>()))
                .Returns(mockResponse);

            var target = new HttpClient(mockSocket.Object);

            // execution
            var result = target.Send(new HttpRequest(""));

            // assertion
            Assert.AreEqual(content, result, "The http reponse was not correct");
        }
开发者ID:matt-barton,项目名称:NETMF.Utilities,代码行数:24,代码来源:SendTest.cs


注:本文中的HttpClient.Send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。