Other Nancy.Testing.Browser GET/PUT/POST/DELETE

本文关键字:PUT DELETE POST GET Nancy Testing Browser Other | 更新日期: 2023-09-27 18:36:51

Using the Nancy Framework...http://nancyfx.org/

如果我想在客户端使用浏览器对象来使用 Nancy 服务,如我们在本例中看到的那样:https://github.com/NancyFx/Nancy/wiki/Testing-your-application

    ...
    var bootstrapper = new DefaultNancyBootstrapper();
    var browser = new Browser(bootstrapper);
    // When
    var result = browser.Get("/", with => {
        with.HttpRequest();
    });
    ...

即使我的应用未测试,我是否必须使用 Nancy.testing???换句话说,是否存在与此对象一样执行获取、放置、发布和删除操作的其他浏览器对象???

Other Nancy.Testing.Browser GET/PUT/POST/DELETE

你想要一些东西来实际使用服务吗?看看 EasyHttp 或 RestSharp - 它们都提供了很好的 API 来消费 HTTP API。

我发现类System.Net.WebClient也执行GET/PUT/POST/DELETE例如

//Creating client instance and set the credentials
var client = new WebClient();
client.Credentials = new NetworkCredential(...);
// using GET Request:
var data = client.DownloadData("http://myurl/.../" + docId);
// Using PUT
var data = Encoding.UTF8.GetBytes("My text goes here!");
client.UploadData("http://myurl/...", "PUT", data);
// Using POST
var data = new NameValueCollection();
data.Add("Field1", "value1");
data.Add("Field2", "value2");
client.UploadValues("http://myurl/...", "POST", data);

但是,最后我决定将WCF REST客户端与webHttpBinding一起使用。像这样:

[ServiceContract]
public interface IMyService
{
     [OperationContract]
     [WebGet(UriTemplate = "{docId}")]
     void GetData(string docId);
 }

混凝土等级:

class MyClient: ClientBase<IMyService>, IMyService
{
    public void GetData(string docId)
    {
        Channel.GetData(docId);
    }
}