本文整理汇总了C#中RestClient.ExecuteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.ExecuteAsync方法的具体用法?C# RestClient.ExecuteAsync怎么用?C# RestClient.ExecuteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.ExecuteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultipleRequestsFromSameClientShouldNotFail
public async Task MultipleRequestsFromSameClientShouldNotFail()
{
// Setup
var client = new RestClient { BaseUrl = BaseAddress };
var request = new RestRequest("api/books");
List<Book> response;
// Execute
using (WebApp.Start<WebApiStartup>(BaseAddress))
{
response = await client.ExecuteAsync<List<Book>>(request);
}
// Validate
response.Should().NotBeNull();
response.Count().Should().Be(5);
var request2 = new RestRequest("api/books");
List<Book> response2;
// Execute
using (WebApp.Start<WebApiStartup>(BaseAddress))
{
response2 = await client.ExecuteAsync<List<Book>>(request2);
}
// Validate
response2.Should().NotBeNull();
response2.Count().Should().Be(5);
}
示例2: Handles_Server_Timeout_Error_Async
public void Handles_Server_Timeout_Error_Async()
{
const string baseUrl = "http://localhost:8888/";
var resetEvent = new ManualResetEvent(false);
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("404") { Timeout = 500 };
IRestResponse response = null;
client.ExecuteAsync(request, responseCb =>
{
response = responseCb;
resetEvent.Set();
});
resetEvent.WaitOne();
Assert.NotNull(response);
Assert.Equal(response.ResponseStatus, ResponseStatus.TimedOut);
Assert.NotNull(response.ErrorException);
Assert.IsAssignableFrom(typeof(WebException), response.ErrorException);
Assert.Equal(response.ErrorException.Message, "The request timed-out.");
}
}
开发者ID:mgulubov,项目名称:RestSharpHighQualityCodeTeamProject,代码行数:26,代码来源:NonProtocolExceptionHandlingTests.cs
示例3: Can_Perform_GET_Async
public void Can_Perform_GET_Async()
{
const string baseUrl = "http://localhost:8084/";
const string val = "Basic async test";
var resetEvent = new ManualResetEvent(false);
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
IRestResponse response = null;
client.ExecuteAsync(request, (resp, asyncHandle) =>
{
response = resp;
resetEvent.Set();
});
resetEvent.WaitOne();
Assert.NotNull(response.Content);
Assert.Equal(val, response.Content);
}
}
示例4: Authenticate
private async Task Authenticate()
{
var client = new RestClient
{
BaseUrl = "https://datamarket.accesscontrol.windows.net",
UserAgent = UserAgent,
};
var request = new RestRequest("v2/OAuth2-13", HttpMethod.Post)
{
ContentType = ContentTypes.FormUrlEncoded,
ReturnRawString = true,
};
request.AddParameter("client_id", ClientId);
request.AddParameter("client_secret", ClientSecret);
request.AddParameter("scope", "http://music.xboxlive.com");
request.AddParameter("grant_type", "client_credentials");
var result = await client.ExecuteAsync<string>(request);
TokenResponse = JsonConvert.DeserializeObject<TokenResult>(result);
if (TokenResponse != null)
{
TokenResponse.TimeStamp = DateTime.Now;
}
}
示例5: GetResourceAsync
public async Task<byte[]> GetResourceAsync(Uri uri)
{
var client = new RestClient();
var request = new RestRequest(uri.AbsoluteUri, HttpMethod.Get, ContentTypes.ByteArray);
var result = await client.ExecuteAsync<byte[]>(request);
return result;
}
示例6: Can_Handle_Uncompressed_Content
public async Task Can_Handle_Uncompressed_Content()
{
const string baseUrl = "http://localhost:8080/";
using (HttpServer server = new HttpServer(8080, request => Handlers.EchoValue(request.Path) ))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("uncompressed.json.txt");
var response = await client.ExecuteAsync(request);
Assert.AreEqual("This is some sample content", response.Content);
}
}
示例7: Handles_GET_Request_404_Error
public async Task Handles_GET_Request_404_Error()
{
const string baseUrl = "http://localhost:8080/";
using (HttpServer server = new HttpServer(8080, request => UrlToStatusCodeHandler(request.Path) ))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("404");
var response = await client.ExecuteAsync(request);
Assert.AreEqual((int)HttpStatusCode.NotFound, response.StatusCode);
}
}
示例8: Can_Authenticate_With_Basic_Http_Auth
public async Task Can_Authenticate_With_Basic_Http_Auth()
{
const string baseUrl = "http://localhost:8080/";
using (HttpServer server = new HttpServer(8080, request => UsernamePasswordEchoHandler(request) ))
{
var client = new RestClient(baseUrl);
client.Authenticator = new HttpBasicAuthenticator("testuser", "testpassword");
var request = new RestRequest("basicAuth.json.txt");
var response = await client.ExecuteAsync(request);
Assert.AreEqual("testuser|testpassword", response.Content);
}
}
示例9: Can_Handle_Deflate_Compressed_Content
public async Task Can_Handle_Deflate_Compressed_Content()
{
const string baseUrl = "http://localhost:8080/";
using (HttpServer server = new HttpServer(8080, request => DeflateEchoValue(request.Path)))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("deflate.json.txt");
var response = await client.ExecuteAsync(request);
System.Diagnostics.Debug.WriteLine("CONTENT: " + response.Content);
Assert.AreEqual("This is some deflated content", response.Content);
}
}
示例10: MultipartFormDataAsync
public void MultipartFormDataAsync()
{
const string baseUrl = "http://localhost:8080/";
using (SimpleServer.Create(baseUrl, EchoHandler)) {
var client = new RestClient(baseUrl);
var request = new RestRequest("/");
request.Method = Method.POST;
request.AlwaysMultipartFormData = true;
AddParameters(request);
var restRequestAsyncHandle = client.ExecuteAsync(request, (restResponse, handle) => {
Console.WriteLine(restResponse.Content);
Assert.True(restResponse.Content == Expected);
});
}
}
示例11: TestMethod1
public void TestMethod1()
{
//var client = new RestClient("https://api.twilio.com");
//client.Authenticator = new HttpBasicAuthenticator("ACec243f00aaaa1b67fa32ae88178ebc2c", "09772adb6b86ba9341168d5277d18fb2");
//var request = new RestRequest("2010-10-01/Accounts/ACec243f00aaaa1b67fa32ae88178ebc2c");
var client = new RestClient("http://bing.com");
var request = new RestRequest();
client.ExecuteAsync(request, r => {
Debug.WriteLine(r.Content);
});
}
示例12: Can_Have_No_Body_Added_To_POST_Request
public void Can_Have_No_Body_Added_To_POST_Request()
{
const Method httpMethod = Method.POST;
using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
RestClient client = new RestClient(BASE_URL);
RestRequest request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);
ManualResetEvent resetEvent = new ManualResetEvent(false);
client.ExecuteAsync(request, response => resetEvent.Set());
resetEvent.WaitOne();
AssertHasNoRequestBody();
}
}
示例13: Can_Perform_GET_Async
public async Task Can_Perform_GET_Async()
{
const string baseUrl = "http://localhost:8080/";
const string val = "Basic async test";
var resetEvent = new ManualResetEvent(false);
//using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
using (HttpServer server = new HttpServer(8080, request => Handlers.EchoValue(request.Path)))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
var response = await client.ExecuteAsync(request);
Assert.IsNotNull(response.Content);
Assert.AreEqual(val, response.Content);
}
}
示例14: ExecuteAsyncOfTReturnsDeserializedContentOfT
public async Task ExecuteAsyncOfTReturnsDeserializedContentOfT()
{
// Setup
var client = new RestClient { BaseUrl = BaseAddress };
var request = new RestRequest("api/books");
List<Book> response;
// Execute
using (WebApp.Start<WebApiStartup>(BaseAddress))
{
response = await client.ExecuteAsync<List<Book>>(request);
}
// Validate
response.Should().NotBeNull();
response.Count().Should().Be(5);
}
示例15: MultipartFormDataAsync
public void MultipartFormDataAsync()
{
const string BaseUrl = "http://localhost:8888/";
using (SimpleServer.Create(BaseUrl, EchoHandler))
{
var client = new RestClient(BaseUrl);
var request = new RestRequest("/", Method.POST) { AlwaysMultipartFormData = true };
this.AddParameters(request);
client.ExecuteAsync(request, (restResponse, handle) =>
{
Console.WriteLine(restResponse.Content);
Assert.Equal(this.expected, restResponse.Content);
});
}
}