本文整理汇总了C#中RestClient.Post方法的典型用法代码示例。如果您正苦于以下问题:C# RestClient.Post方法的具体用法?C# RestClient.Post怎么用?C# RestClient.Post使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RestClient
的用法示例。
在下文中一共展示了RestClient.Post方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RequestContentStreamIsDisposedAutomatically
public void RequestContentStreamIsDisposedAutomatically()
{
var client = new RestClient("http://localhost/abc");
var memoryStream = new TracksWhenDisposedMemoryStream();
client.Post("something", new StreamBodyContent(memoryStream, "foo/bar"), ExpectStatus.IgnoreStatus);
Assert.That(memoryStream.DisposedCount, Is.EqualTo(1));
}
示例2: Discontinue
public ActionResult Discontinue(ProductViewModel product)
{
RestClient<Product> productsRestClient = new RestClient<Product>("http://localhost:3001/");
var result = productsRestClient.Post("products/code/" + product.Code + "/discontinue", new Product { Id = product.Id }).Result;
return RedirectToAction("Index")
.WithSuccess("Product discontinued!");
}
示例3: ShouldPostMultuPartWithBase64Content
public void ShouldPostMultuPartWithBase64Content()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
server.UseBodyExtractor(extractor);
server.Listen();
string message = "Hello, World!Ӽ!";
byte[] messageRaw = Encoding.UTF8.GetBytes(message);
string base64 = Convert.ToBase64String(messageRaw);
byte[] base64Raw = Encoding.UTF8.GetBytes(base64);
NameValueCollection headers = new NameValueCollection();
headers.Add("Content-Transfer-Encoding", "base64");
RestClient client = new RestClient("http://localhost:8080");
client.Post("api/customers")
.WithMultiPartBody(b =>
{
b.WithFile("file", "file.txt", base64Raw, "text/plain", headers);
})
.Execute();
var file = extractor.Files.GetFiles("file").Single();
CollectionAssert.AreEqual(base64Raw, file.Contents);
}
}
示例4: ShouldPostMultuPartWithInvalidCharacter
public void ShouldPostMultuPartWithInvalidCharacter()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
server.UseBodyExtractor(extractor);
server.Listen();
RestClient client = new RestClient("http://localhost:8080");
client.Post("api/customers")
.WithMultiPartBody(b =>
{
b.WithFormData(ub => ub.WithParameter("naӼme", "John Smith"));
})
.Execute();
Assert.AreEqual("John Smith", extractor.Parameters["na?me"], "The form data was not transfered.");
}
}
示例5: ShouldPOSTMultiPartDataAsync
public async Task ShouldPOSTMultiPartDataAsync()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
MultiPartBodyExtractor extractor = new MultiPartBodyExtractor();
server.UseBodyExtractor(extractor);
server.Listen();
RestClient client = new RestClient("http://localhost:8080");
await client.Post("api/customers")
.WithMultiPartBody(b =>
{
b.WithFormData(ub => ub.WithParameter("name", "John Smith"));
b.WithFile("file1", "path", Encoding.UTF8.GetBytes("Hello, world"), "text/plain");
})
.ExecuteAsync();
Assert.AreEqual("John Smith", extractor.Parameters["name"], "The form data was not transfered.");
var file = extractor.Files.GetFiles("file1").SingleOrDefault();
Assert.AreEqual("file1", file.Name);
Assert.AreEqual("path", file.FileName);
Assert.AreEqual("text/plain", file.ContentType);
Assert.AreEqual("Hello, world", Encoding.UTF8.GetString(file.Contents));
}
}
示例6: ShouldPOSTWithNoBody
public void ShouldPOSTWithNoBody()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
RequestExtractor extractor = new RequestExtractor();
server.UseBodyExtractor(extractor);
server.Listen();
RestClient client = new RestClient();
var response = client.Post("http://localhost:8080/api/customers")
.WhenError(r => { throw new Exception(r.FromString<string>()); })
.Execute();
string contentLength = extractor.Headers["Content-Length"];
Assert.AreEqual("0", contentLength, "The content length was not specified.");
}
}
示例7: ShouldPOSTWithJsonData
public void ShouldPOSTWithJsonData()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
var bodyExtractor = new JsonBodyExtractor<TestCustomer>();
server.UseBodyExtractor(bodyExtractor);
server.Listen();
RestClient client = new RestClient();
var response = client.Post("http://localhost:8080/api/customers")
.WithJsonBody(new TestCustomer() { Name = "Bob Smith", Age = 31, Title = "Mr." })
.Execute();
var customer = bodyExtractor.Result;
Assert.AreEqual("Bob Smith", customer.Name, "The name was not sent.");
Assert.AreEqual(31, customer.Age, "The age was not sent.");
Assert.AreEqual("Mr.", customer.Title, "The title was not sent.");
}
}
示例8: ShouldPOSTWithArrayFormData
public void ShouldPOSTWithArrayFormData()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
var bodyExtractor = new UrlEncodedBodyExtractor();
var extractor = new RequestExtractor(bodyExtractor);
server.UseBodyExtractor(extractor);
server.Listen();
RestClient client = new RestClient();
var response = client.Post("http://localhost:8080/api/customers")
.WithUrlEncodedBody(b => b
.WithParameter("CustomerId", 1)
.WithParameter("CustomerId", 2)
.WithParameter("CustomerId", 3))
.Execute();
string[] ids = bodyExtractor.Parameters.GetValues("CustomerId");
string[] expectedIds = new string[] { "1", "2", "3" };
CollectionAssert.AreEquivalent(expectedIds, ids, "The array of values were not sent.");
}
}
示例9: ShouldPOSTWithFormDataObject
public void ShouldPOSTWithFormDataObject()
{
using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers"))
{
var bodyExtractor = new UrlEncodedBodyExtractor();
var extractor = new RequestExtractor(bodyExtractor);
server.UseBodyExtractor(extractor);
server.Listen();
RestClient client = new RestClient();
var response = client.Post("http://localhost:8080/api/customers")
.WithUrlEncodedBody(new
{
Name = "Bob Smith",
Age = 31,
Title = "Mr."
})
.Execute();
string name = bodyExtractor.Parameters["Name"];
string age = bodyExtractor.Parameters["Age"];
string title = bodyExtractor.Parameters["Title"];
Assert.AreEqual("Bob Smith", name, "The name was not sent.");
Assert.AreEqual("31", age, "The age was not sent.");
Assert.AreEqual("Mr.", title, "The title was not sent.");
}
}