本文整理汇总了C#中RestClient.GetAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.GetAsync方法的具体用法?C# RestClient.GetAsync怎么用?C# RestClient.GetAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.GetAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAsyncReturnsHttpResponseExceptionTest
public async Task GetAsyncReturnsHttpResponseExceptionTest()
{
var responseMessage = HttpClientHelper.SetupHttpResponseMessage(HttpStatusCode.NotFound, new JsonContent(new List<TestResponseClass>()));
var fakeMessageHandler = new FakeHttpMessageHandler(responseMessage);
var httpClient = HttpClientHelper.SetupHttpClient(fakeMessageHandler);
var sut = new RestClient(httpClient);
await sut.GetAsync<object>("GetAsync");
}
示例2: GetResource_RelativeRequestUriAndMissingBaseAddress_ThrowsImmediately
public async Task GetResource_RelativeRequestUriAndMissingBaseAddress_ThrowsImmediately()
{
// Arrange
var client = new RestClient();
var task = client.GetAsync<Post>(new Uri("post/1", UriKind.Relative));
// Assert
await task.AssertThrows<InvalidOperationException>();
}
示例3: GetAsyncReturnsCorrectClassTypeTest
public async Task GetAsyncReturnsCorrectClassTypeTest()
{
var responseMessage = HttpClientHelper.SetupHttpResponseMessage(HttpStatusCode.OK, new JsonContent(new List<TestResponseClass>()));
var fakeMessageHandler = new FakeHttpMessageHandler(responseMessage);
var httpClient = HttpClientHelper.SetupHttpClient(fakeMessageHandler);
var sut = new RestClient(httpClient);
var result = await sut.GetAsync<TestResponseClass>("GetAsync");
Assert.AreEqual(typeof(List<TestResponseClass>), result.GetType());
}
示例4: Should_get_bson
public async Task Should_get_bson()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
var foos = await restClient.GetAsync<Foo>("/api/foos/{0}".FormatUri(1), MediaTypes.ApplicationBson);
foos.ShouldNotBeNull();
}
}
示例5: Should_get_xml
public async Task Should_get_xml()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
var foos = await restClient.GetAsync<List<Foo>>("/api/foos", MediaTypes.ApplicationXml);
foos.Count.ShouldEqual(3);
}
}
示例6: RestClientCallsCorrectRouteTest
public async Task RestClientCallsCorrectRouteTest()
{
var responseMessage = HttpClientHelper.SetupHttpResponseMessage(HttpStatusCode.OK, new JsonContent(new List<TestResponseClass>()));
var fakeMessageHandler = new FakeHttpMessageHandler(responseMessage);
var httpClient = HttpClientHelper.SetupHttpClient(fakeMessageHandler, new Uri("http://localhost/WebApi/"));
var sut = new RestClient(httpClient);
await sut.GetAsync<TestResponseClass>("GetAsync");
Assert.AreEqual(fakeMessageHandler.Request.RequestUri, "http://localhost/WebApi/GetAsync");
}
示例7: Should_get_bson_simple_type
public async Task Should_get_bson_simple_type()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
var parameters = new Dictionary<string, string> { { "param", "a_param" } };
var foo = await restClient.GetAsync<int>("/api/foos", null, parameters);
foo.ShouldNotEqual(0);
}
}
示例8: GetResourceList_Succeeds
public void GetResourceList_Succeeds()
{
// Arrange
var client = new RestClient(_baseAddress);
// Act
var response = client.GetAsync<IEnumerable<Post>>(new Uri("posts", UriKind.Relative)).Result;
// Assert
Assert.IsTrue(response.Succeeded);
Assert.IsTrue(response.Data.Any());
}
示例9: GetResourceList_Fails
public async Task GetResourceList_Fails()
{
// Arrange
var client = new RestClient(_incorrectBaseAddress);
// Act
var response = await client.GetAsync<IEnumerable<Post>>(new Uri("posts", UriKind.Relative));
// Assert
Assert.IsFalse(response.Succeeded);
Assert.IsTrue(response.Data == null);
}
示例10: Should_handle_not_found_when_getting_json
public void Should_handle_not_found_when_getting_json()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
var aggregateException = Assert.Throws<AggregateException>(() => restClient.GetAsync<object>("/api/unknown", MediaTypes.ApplicationJson).Result);
aggregateException.InnerException.ShouldBeType<ApiException>();
var apiException = (ApiException)aggregateException.InnerException;
apiException.HttpStatusCode.ShouldEqual(HttpStatusCode.NotFound);
}
}
示例11: Should_handle_error_when_getting_xml
public void Should_handle_error_when_getting_xml()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
const string ErrorMessage = "error";
var aggregateException = Assert.Throws<AggregateException>(() => restClient.GetAsync<object>("/api/error/" + ErrorMessage, MediaTypes.ApplicationXml).Result);
aggregateException.InnerException.ShouldBeType<ApiException>();
var apiException = (ApiException)aggregateException.InnerException;
apiException.ApiError.Message.ShouldEqual(ErrorMessage);
}
}
示例12: GetAsync
public async Task GetAsync()
{
var init = Substitute.For<IHttpInitializer>();
var messager = new FakeMessageHandler(new HttpResponseMessage());
init.HttpClient.Returns(new HttpClient(messager));
var rest = new RestClient(init);
var config = new HttpConfiguration(new Uri("http://happy.ca/"), "route", HttpRequest.Get);
var ads = await rest.GetAsync(config, response => new List<Ad>{new Ad{AdID = Int32.MaxValue}});
Assert.AreEqual(1, ads.Count(x=>x.AdID==Int32.MaxValue));
Assert.AreEqual("http://happy.ca/route", messager.RequestMessage.RequestUri.AbsoluteUri);
Assert.AreEqual(HttpMethod.Get, messager.RequestMessage.Method);
}
示例13: QueryAsync
async static Task QueryAsync()
{
var client = new RestClient("http://catfacts-api.appspot.com/api");
var response = await client.GetAsync<CatFactResponse>(
new RestRequest("facts")
.WithQueryParameter("number", 5)
);
Console.WriteLine("Retrieved " + response.Facts.Count + " cat facts.");
Console.WriteLine(response.Facts[0]);
Console.ReadKey();
}
示例14: GetAsyncReturnsCorrectPopulatedClassTest
public async Task GetAsyncReturnsCorrectPopulatedClassTest()
{
var testResponseClassList = new List<TestResponseClass>
{
new TestResponseClass {ResponseContent = "content1"},
new TestResponseClass {ResponseContent = "content2"},
new TestResponseClass {ResponseContent = "content3"}
};
var responseMessage = HttpClientHelper.SetupHttpResponseMessage(HttpStatusCode.OK, new JsonContent(testResponseClassList));
var fakeMessageHandler = new FakeHttpMessageHandler(responseMessage);
var httpClient = HttpClientHelper.SetupHttpClient(fakeMessageHandler);
var sut = new RestClient(httpClient);
var result = await sut.GetAsync<TestResponseClass>("GetAsync");
var resultList = result.ToList();
Assert.AreEqual(testResponseClassList[0].ResponseContent, resultList[0].ResponseContent);
Assert.AreEqual(testResponseClassList[1].ResponseContent, resultList[1].ResponseContent);
Assert.AreEqual(testResponseClassList[2].ResponseContent, resultList[2].ResponseContent);
}
示例15: Should_throw_exception_for_unkown_client_mediatype
public void Should_throw_exception_for_unkown_client_mediatype()
{
using (var server = TestServer.Create<Startup>())
{
var restClient = new RestClient(server.HttpClient);
restClient.MediaTypeSerializers.Remove(restClient.MediaTypeSerializers.First(x => x is JsonMediaTypeSerializer));
var aggregateException = Assert.Throws<AggregateException>(() =>
{
var result = restClient.GetAsync<List<Foo>>("/api/foos", MediaTypes.ApplicationJson).Result;
return result;
});
aggregateException.InnerException.ShouldBeType<NotSupportedException>();
}
}