【问题标题】:Other Nancy.Testing.Browser GET/PUT/POST/DELETE其他 Nancy.Testing.Browser GET/PUT/POST/DELETE
【发布时间】:2012-06-28 22:29:09
【问题描述】:

使用 Nancy 框架...http://nancyfx.org/

如果我想在客户端使用 Browser 对象来消费 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?换句话说,是否存在像这个对象一样执行 Get、Put、Post 和 Delete 操作的其他浏览器对象???

【问题讨论】:

    标签: c# nancy


    【解决方案1】:

    您想要一些东西来实际使用服务吗?看看EasyHttpRestSharp - 它们都提供了很好的 API 来使用 HTTP API。

    【讨论】:

      【解决方案2】:

      我发现 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);
      

      但是,最后我决定使用带有webHttpBinding 的 WCF REST 客户端。像这样的:

      [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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-25
        • 1970-01-01
        • 2013-09-27
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-17
        • 2018-05-23
        相关资源
        最近更新 更多