本文整理汇总了C#中RestRequest.GetResponse方法的典型用法代码示例。如果您正苦于以下问题:C# RestRequest.GetResponse方法的具体用法?C# RestRequest.GetResponse怎么用?C# RestRequest.GetResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestRequest
的用法示例。
在下文中一共展示了RestRequest.GetResponse方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAborted
public void GetAborted()
{
//Arrange
StubModule.HaltProcessing = TimeSpan.FromMilliseconds(1000);
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
RestResponse response = null;
//A completely synchronous GetResponse cannot be aborted.
//So, spining off a new thread.. And balancing the times between GetResponse() and Abort() methods.
var task = Task.Factory.StartNew(() =>
{
response = target.GetResponse();
});
Thread.Sleep(500);
target.Abort();
task.Wait();
//Assert
Assert.NotNull(response);
Assert.NotNull(response.Error);
Assert.Equal("The request was aborted: The request was canceled.", response.Error.InnerException.Message);
Assert.Equal(0, (int)response.Error.StatusCode);
}
示例2: Get
public void Get()
{
//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
using (RestResponse<Person> actual = target.GetResponse<Person>())
{
//Assert
Assert.True(StubModule.GetPerson);
Assert.NotNull(actual);
Assert.NotNull(actual.Data);
Assert.True(actual.IsSuccessStatusCode);
Person person = actual.Data;
Assert.Equal("[email protected]", person.Email);
}
}
示例3: Delete
public void Delete()
{
StubModule.HaltProcessing = TimeSpan.FromSeconds(0);
StubModule.DeletePerson = false;
StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };
RestRequest target = new RestRequest(HttpMethod.DELETE, new RestUri(_MyUri, "/Person/{id}").SetParameter("id", "1"));
using (RestResponse actual = target.GetResponse())
{
Assert.True(StubModule.DeletePerson);
Assert.NotNull(actual);
Assert.True(actual.IsSuccessStatusCode);
var person = StubModule.TestHarness.Where(x => x.Id == 1).FirstOrDefault();
Assert.Null(person);
}
}
示例4: PatchWithNullBody
public void PatchWithNullBody()
{
StubModule.HaltProcessing = TimeSpan.FromSeconds(0);
StubModule.PatchPerson = false;
StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };
RestRequest target = new RestRequest(HttpMethod.PATCH, new RestUri(_MyUri, "/Person/{id}").SetParameter("id", "1"));
target.ContentType = ContentType.ApplicationJson;
target.Body = null;
using (RestResponse actual = target.GetResponse())
{
Assert.True(StubModule.PatchPerson);
Assert.NotNull(actual);
Assert.True(actual.IsSuccessStatusCode);
}
}
示例5: PatchWithJsonContentType
public void PatchWithJsonContentType()
{
StubModule.HaltProcessing = TimeSpan.FromSeconds(0);
StubModule.PatchPerson = false;
StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };
RestRequest target = new RestRequest(HttpMethod.PATCH, new RestUri(_MyUri, "/Person/{id}").SetParameter("id", "1"));
target.ContentType = ContentType.ApplicationJson;
target.Body = new RestObjectRequestBody<Person>(new Person { Id = 1, Email = "[email protected]" });
using (RestResponse actual = target.GetResponse())
{
Assert.True(StubModule.PatchPerson);
Assert.NotNull(actual);
Assert.True(actual.IsSuccessStatusCode);
var person = StubModule.TestHarness.Where(x => x.Id == 1).FirstOrDefault();
Assert.NotNull(person);
Assert.Equal("[email protected]", person.Email);
}
}
示例6: PostWithNormalContentType
public void PostWithNormalContentType()
{
StubModule.HaltProcessing = TimeSpan.FromSeconds(0);
StubModule.PostPerson = false;
StubModule.TestHarness = new List<Person> { new Person { Id = 1, Email = "[email protected]" } };
RestRequest target = new RestRequest(HttpMethod.POST, new RestUri(_MyUri, "/Person"));
target.ContentType = ContentType.ApplicationX_WWW_Form_UrlEncoded;
target.Body = new RestObjectRequestBody<Person>(new Person { Id = 2, Email = "[email protected]" });
using (RestResponse actual = target.GetResponse())
{
Assert.True(StubModule.PostPerson);
Assert.NotNull(actual);
Assert.True(actual.IsSuccessStatusCode);
var person = StubModule.TestHarness.Where(x => x.Id == 2).FirstOrDefault();
Assert.NotNull(person);
Assert.Equal("[email protected]", person.Email);
}
}
示例7: GetWeaklyTypedResponse
public void GetWeaklyTypedResponse()
{
//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
using (RestResponse actual = target.GetResponse())
{
//Assert
Assert.True(actual.IsSuccessStatusCode);
Assert.True(StubModule.GetPerson);
Assert.NotNull(actual);
string content = actual.Body.ReadAsString();
Assert.Equal("{\"Id\":1,\"UID\":\"00000000-0000-0000-0000-000000000000\",\"Email\":\"[email protected]\",\"NoOfSiblings\":0,\"DOB\":\"\\/Date(-59011459200000)\\/\",\"IsActive\":false,\"Salary\":0}", content);
}
}
示例8: GetWithoutResolvableDomainName
public void GetWithoutResolvableDomainName()
{
//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.nripendraiscool:60001/api"), "/Person/{id}").SetParameter("id", "1"));
//Act
using (RestResponse<Person> actual = target.GetResponse<Person>())
{
//Assert
Assert.NotNull(actual);
Assert.NotNull(actual.Error);
Assert.NotNull(actual.Error.InnerException);
Assert.Equal("the remote name could not be resolved: 'localhost.nripendraiscool'", actual.Error.InnerException.Message.ToLower());
}
}
示例9: GetOfTWithoutServer
public void GetOfTWithoutServer()
{
//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.GetResponse<Person>())
{
//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());
}
}
示例10: GetOfTWithTimeout
public void GetOfTWithTimeout()
{
//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<Person> actual = target.GetResponse<Person>())
{
//Assert
Assert.True(StubModule.GetPerson);
Assert.NotNull(actual);
Assert.NotNull(actual.Error);
Assert.NotNull(actual.Error.InnerException);
Assert.Equal("the operation has timed out", actual.Error.InnerException.Message.ToLower());
}
}
示例11: Simple
public void Simple()
{
string location = AppDomain.CurrentDomain.BaseDirectory;
string boundary = Guid.NewGuid().ToString();
RestRequest request = new RestRequest(HttpMethod.POST, new RestUri(_MyUri, "/File"));
request.ContentType = ContentType.MultiPartFormData.WithBoundary(boundary);
string[] files = new string[]
{
Path.Combine(location, "TextFile1.txt"),
Path.Combine(location, "TextFile2.txt"),
};
IDictionary<string, string> formData = new Dictionary<string, string>
{
{"Id", "1"},
{"Email", "[email protected]"},
};
StringBuilder sb = new StringBuilder();
foreach (var file in files)
{
sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"media\"; filename=\"" + Path.GetFileName(file) + "\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Type: " + MimeTypes.GetMimeType(Path.GetExtension(file)) ?? ContentType.ApplicationOctetStream.ToString());
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
byte[] contents = new byte[fs.Length];
fs.Read(contents, 0, contents.Length);
sb.Append(Encoding.Default.GetString(contents));
}
sb.AppendFormat("\r\n");
}
foreach (var keyvaluepair in formData)
{
sb.AppendFormat("--{0}", boundary);
sb.AppendFormat("\r\n");
sb.AppendFormat("Content-Disposition: form-data; name=\"" + keyvaluepair.Key + "\"");
sb.AppendFormat("\r\n");
sb.AppendFormat("\r\n");
sb.AppendFormat(keyvaluepair.Value);
sb.AppendFormat("\r\n");
}
sb.AppendFormat("--{0}--", boundary);
byte[] fulldata = Encoding.Default.GetBytes(sb.ToString());
var body = new RestRawRequestBody(fulldata);
request.Body = body;
var response = request.GetResponse<List<string>>();
var data = response.Data;
Assert.Equal(2, data.Count);
Assert.Equal("Hello world from text file 1!", data[0]);
Assert.Equal("Hello world from text file 2!", data[1]);
}