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


C# RestRequest.GetResponseAsync方法代码示例

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


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

示例1: RestRequest_Raw

        public static async void RestRequest_Raw()
        {
            ///
            /// First a test using the "raw" RestRequest class.
            ///
            RestRequest request = new RestRequest();
            string url = "https://duckduckgo.com/";

            RestResponse<IVoid> response = await request
                .Get()                              // Set HTTP method to "GET"
                .Url(url)                           // Set our url
                .QParam("q", "RestlessHttpClient")  // Set a request query parameter
                .Fetch();                           // Get response, without serialization

            if (!response.IsSuccessStatusCode)
            {
                HttpStatusCode status = response.HttpResponse.StatusCode;
                //...
            }
            else if (response.IsException)
                Debug.WriteLine(response.Exception.Message);
            else
            {
                HttpContent content = response.HttpResponse.Content;
                string strContent = await content.ReadAsStringAsync();
                // ...
            }


            // Get http://www.example.com/Person?name=<PersonName> 
            // Example Person class 
            // class Person
            // {
            //    string Name{get;set;}
            //    int    Age{get; set;}
            // }
            url = "http://www.example.com/Person";

            var getPerson = new RestRequest();
            RestResponse<Person> personResponse = await getPerson
                .Get(url)    // HTTP Method "GET" and set the url for the Person get REST endpoint
                .QParam("name", "TestUser")     // The api wants the person name as query parameter
                .Fetch<Person>();                // Get response and deserialize to Person object.

            if (personResponse.HasData)
            {
                Person person = personResponse.Data;
                //...
            }
            else
            {
                // Error handling
                // Check personResponse.isStatusCodeMissmatch and personResponse.isException
            }

            // POST http://www.example.com/Person
            // name=<name>,age=<age>

            var createPerson = new RestRequest();

            // Add form url encoded content, all params that are added before the AddFormUrl call will be
            // added automatically. 
            createPerson.
                Post().                             // Set HTTP method to "POST", we are creating a new Person
                Url(url).                           // Set Url
                Param("name", "NewUser").           // The new person name as parameter
                Param("age", 99).                   // The new person age as parameter
                AddFormUrl();                       // Add all currently added parameter as Form Url parameter

            // or equivalent
            //createPerson.Post().Url(url).AddFormUrl("name", "NewUser", "age", 99.ToString());        

            // Now create the new person and get the response.
            RestResponse<IVoid> createResponse = await createPerson.Fetch();

            // createResponse.Response is the underlying HttpResponseMessage
            if (createResponse.HttpResponse.StatusCode != HttpStatusCode.Created)
            {
                // do error handling
            }

            // or get the HttpResponseMessage directly if no deserialization is needed.
            HttpResponseMessage httpResponse = await createPerson.GetResponseAsync();

            if (httpResponse.StatusCode != HttpStatusCode.Created)
            {
                // Error handling
            }


            // Do an action on the underlying HttpRequestMessage
            request = new RestRequest().
                RequestAction(r => r.Headers.Host = "http://www.test.com").
                RequestAction(r => r.Method = new HttpMethod("GET"));

            request.ClientAction((c) => c.Timeout = new TimeSpan(50000));

            // Fetch request with success and error actions.
            await getPerson.Fetch<Person>(
                (r) => Debug.WriteLine(r.Data.Name + " is " + r.Data.Age + " years old."),
//.........这里部分代码省略.........
开发者ID:mdabbagh88,项目名称:Restless,代码行数:101,代码来源:Sample.cs

示例2: GetAsyncWithTimeout

        public void GetAsyncWithTimeout()
        {
            //Arrange
            StubModule.HaltProcessing = TimeSpan.FromSeconds(10);
            StubModule.GetPerson = false;
            StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };

            RestRequest target = new RestRequest(HttpMethod.GET, new RestUri(_MyUri, "/Person/{id}").SetParameter("id", "1"));
            target.TimeOut = TimeSpan.FromSeconds(2);

            //Act
            using (RestResponse actual = target.GetResponseAsync().Result)
            {
                //Assert
                Assert.True(StubModule.GetPerson);
                Assert.NotNull(actual);
                Assert.NotNull(actual.Error.InnerException);
                Assert.Equal("the operation has timed out", actual.Error.InnerException.Message.ToLower());
            }
        }
开发者ID:nripendra,项目名称:Resty.Net,代码行数:20,代码来源:RestRequestTest.cs

示例3: GetAsyncOfTWithoutServer

        public void GetAsyncOfTWithoutServer()
        {
            //Arrange
            StubModule.HaltProcessing = TimeSpan.FromSeconds(10);
            StubModule.GetPerson = false;
            StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };

            RestRequest target = new RestRequest(HttpMethod.GET, new RestUri(new Uri("http://localhost:60001/api"), "/Person/{id}").SetParameter("id", "1"));

            //Act
            using (RestResponse<Person> actual = target.GetResponseAsync<Person>().Result)
            {
                //Assert
                Assert.NotNull(actual);
                Assert.NotNull(actual.Error);
                Assert.NotNull(actual.Error.InnerException);
                Assert.Equal("unable to connect to the remote server", actual.Error.InnerException.Message.ToLower());
            }
        }
开发者ID:nripendra,项目名称:Resty.Net,代码行数:19,代码来源:RestRequestTest.cs

示例4: GetAsyncOfTAborted

        public void GetAsyncOfTAborted()
        {
            //Arrange
            StubModule.HaltProcessing = TimeSpan.FromSeconds(0);
            StubModule.GetPerson = false;
            StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };

            RestRequest target = new RestRequest(HttpMethod.GET, new RestUri(_MyUri, "/Person/{id}").SetParameter("id", "1"));

            //Act
            var response = target.GetResponseAsync<Person>();
            target.Abort();

            //Assert
            Assert.NotNull(response.Result);
            Assert.NotNull(response.Result.Error);
            Assert.Equal("The request was aborted: The request was canceled.", response.Result.Error.InnerException.Message);
            Assert.Equal(0, (int)response.Result.Error.StatusCode);
        }
开发者ID:nripendra,项目名称:Resty.Net,代码行数:19,代码来源:RestRequestTest.cs


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