本文整理汇总了C#中RestClient.ExecuteTaskAsync方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.ExecuteTaskAsync方法的具体用法?C# RestClient.ExecuteTaskAsync怎么用?C# RestClient.ExecuteTaskAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.ExecuteTaskAsync方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler
public void Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler()
{
const string baseUrl = "http://localhost:8080/";
const string ExceptionMessage = "Thrown from OnBeforeDeserialization";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("success");
request.OnBeforeDeserialization += response =>
{
throw new Exception(ExceptionMessage);
};
var task = client.ExecuteTaskAsync<Response>(request);
try
{
// In the broken version of the code, an exception thrown in OnBeforeDeserialization causes the task to
// never complete. In order to test that condition, we'll wait for 5 seconds for the task to complete.
// Since we're connecting to a local server, if the task hasn't completed in 5 seconds, it's safe to assume
// that it will never complete.
Assert.True(task.Wait(TimeSpan.FromSeconds(5)), "It looks like the async task is stuck and is never going to complete.");
}
catch (AggregateException e)
{
Assert.Equal(1, e.InnerExceptions.Count);
Assert.Equal(ExceptionMessage, e.InnerExceptions.First().Message);
return;
}
Assert.True(false, "The exception thrown from OnBeforeDeserialization should have bubbled up.");
}
}
示例2: MultipartFormData_WithParameterAndFile_Async
public void MultipartFormData_WithParameterAndFile_Async()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, EchoHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("/", Method.POST)
{
AlwaysMultipartFormData = true
};
DirectoryInfo directoryInfo = Directory.GetParent(Directory.GetCurrentDirectory())
.Parent;
if (directoryInfo != null)
{
string path = Path.Combine(directoryInfo.FullName,
"Assets\\TestFile.txt");
request.AddFile("fileName", path);
}
request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody);
Task task = client.ExecuteTaskAsync(request)
.ContinueWith(x =>
{
Assert.AreEqual(this.expectedFileAndBodyRequestContent, x.Result.Content);
});
task.Wait();
}
}
示例3: Task_Handles_Non_Existent_Domain
public void Task_Handles_Non_Existent_Domain()
{
var client = new RestClient("http://192.168.1.200:8001");
var request = new RestRequest("/")
{
RequestFormat = DataFormat.Json,
Method = Method.GET
};
AggregateException agg = Assert.Throws<AggregateException>(
delegate
{
var response = client.ExecuteTaskAsync<StupidClass>(request);
response.Wait();
});
Assert.IsType(typeof(WebException), agg.InnerException);
Assert.Equal("Unable to connect to the remote server", agg.InnerException.Message);
//var client = new RestClient("http://nonexistantdomainimguessing.org");
//var request = new RestRequest("foo");
//var response = client.ExecuteTaskAsync(request);
//Assert.Equal(ResponseStatus.Error, response.Result.ResponseStatus);
}
示例4: Can_Perform_ExecuteGetTaskAsync_With_Response_Type
public void Can_Perform_ExecuteGetTaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8080/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("success");
var task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
Assert.Equal("Works!", task.Result.Data.Message);
}
}
示例5: Can_Perform_GET_TaskAsync
public void Can_Perform_GET_TaskAsync()
{
const string baseUrl = "http://localhost:8080/";
const string val = "Basic async task test";
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
var task = client.ExecuteTaskAsync(request);
task.Wait();
Assert.NotNull(task.Result.Content);
Assert.Equal(val, task.Result.Content);
}
}
示例6: Can_Cancel_GET_TaskAsync_With_Response_Type
public void Can_Cancel_GET_TaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8080/";
const string val = "Basic async task test";
using (SimpleServer.Create(baseUrl, Handlers.EchoValue(val)))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("timeout");
var cancellationTokenSource = new CancellationTokenSource();
var task = client.ExecuteTaskAsync<Response>(request, cancellationTokenSource.Token);
cancellationTokenSource.Cancel();
Assert.True(task.IsCanceled);
}
}
示例7: Task_Handles_Non_Existent_Domain
public void Task_Handles_Non_Existent_Domain()
{
RestClient client = new RestClient("http://192.168.1.200:8001");
RestRequest request = new RestRequest("/")
{
RequestFormat = DataFormat.Json,
Method = Method.GET
};
Task<IRestResponse<StupidClass>> task = client.ExecuteTaskAsync<StupidClass>(request);
task.Wait();
IRestResponse<StupidClass> response = task.Result;
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual("Unable to connect to the remote server", response.ErrorException.Message);
Assert.AreEqual(ResponseStatus.Error, response.ResponseStatus);
}
示例8: Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler
public void Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler()
{
const string baseUrl = "http://localhost:8888/";
const string ExceptionMessage = "Thrown from OnBeforeDeserialization";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("success");
request.OnBeforeDeserialization += r =>
{
throw new Exception(ExceptionMessage);
};
var task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
var response = task.Result;
Assert.Equal(ExceptionMessage, response.ErrorMessage);
Assert.Equal(ResponseStatus.Error, response.ResponseStatus);
}
}
示例9: Can_Timeout_PUT_TaskAsync
public void Can_Timeout_PUT_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("timeout", Method.PUT).AddBody("Body_Content");
//Half the value of ResponseHandler.Timeout
request.Timeout = 500;
var task = client.ExecuteTaskAsync(request);
task.Wait();
var response = task.Result;
Assert.Equal(ResponseStatus.TimedOut, response.ResponseStatus);
}
}
示例10: Handles_GET_Request_Errors_TaskAsync_With_Response_Type
public void Handles_GET_Request_Errors_TaskAsync_With_Response_Type()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, UrlToStatusCodeHandler))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("404");
var task = client.ExecuteTaskAsync<Response>(request);
task.Wait();
Assert.Null(task.Result.Data);
}
}
示例11: Handles_GET_Request_Errors_TaskAsync
public void Handles_GET_Request_Errors_TaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, UrlToStatusCodeHandler))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("404");
var task = client.ExecuteTaskAsync(request);
task.Wait();
Assert.Equal(HttpStatusCode.NotFound, task.Result.StatusCode);
}
}
示例12: Handles_Server_Timeout_Error_AsyncTask
public void Handles_Server_Timeout_Error_AsyncTask()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, TimeoutHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("404") { Timeout = 500 };
Task<IRestResponse> task = client.ExecuteTaskAsync(request);
task.Wait();
IRestResponse response = task.Result;
Assert.NotNull(response);
Assert.AreEqual(response.ResponseStatus, ResponseStatus.TimedOut);
Assert.NotNull(response.ErrorException);
Assert.IsInstanceOf<WebException>(response.ErrorException);
Assert.AreEqual(response.ErrorException.Message, "The request timed-out.");
}
}
示例13: Can_Timeout_PUT_TaskAsync
public void Can_Timeout_PUT_TaskAsync()
{
const string baseUrl = "http://localhost:8080/";
using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("timeout", Method.PUT).AddBody("Body_Content");
//Half the value of ResponseHandler.Timeout
request.Timeout = 500;
System.AggregateException agg = Assert.Throws<System.AggregateException>(
delegate
{
var task = client.ExecuteTaskAsync(request);
task.Wait();
});
Assert.IsType(typeof(WebException), agg.InnerException);
Assert.Equal("The request timed-out.", agg.InnerException.Message);
}
}
示例14: AlwaysMultipartFormData_WithParameter_ExecuteTaskAsync
public void AlwaysMultipartFormData_WithParameter_ExecuteTaskAsync()
{
const string baseUrl = "http://localhost:8888/";
using (SimpleServer.Create(baseUrl, EchoHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest("?json_route=/posts")
{
AlwaysMultipartFormData = true,
Method = Method.POST,
};
request.AddParameter("title", "test", ParameterType.RequestBody);
Task task = client.ExecuteTaskAsync(request)
.ContinueWith(x => { Assert.Null(x.Result.ErrorException); });
task.Wait();
}
}