2012-06-12 49 views
1

使用南希框架... http://nancyfx.org/其他Nancy.Testing.Browser GET/PUT/POST/DELETE

如果我想使用的浏览器对象在客户端消耗南希服务,就像我们在这看例如: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操作?

回答

1

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

你想要一些实际消耗服务?看看EasyHttpRestSharp - 它们都为使用HTTP API提供了很好的API。