2012-06-12 35 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。