2012-09-03 52 views
3

我是C#的新手,仍然嘗試熟悉它的環境。如何在C#中編寫REST Get-Request?

我想在獲取模式下發出REST請求。給我API接口的人給我提供了以下信息:

HTTP Methods: GET 
Authentication: None 
Formats: xml 
Parameters: format, apikey [GET], lang [GET], q [GET] 
CURL Example: curl --get --data lang="de" --data q="query" --data apikey="QWERTY123456" http://jokr.info/api/v8/search/item.xml 

我不知道如何把它放在C#中。 我試圖使用WebClient但我不知道如何把我的請求與參數的行動。

+0

你能告訴你的WebClient的方法,所以我們可以看看有什麼錯呢? – coolmine

+0

使用谷歌找到一個REST庫。在wikipedia上有一篇關於REST的論文,有助於閱讀。 – mrsteve

回答

1

試試這個

string URI = "http://jokr.info/api/v8/search/item.xml"; 
string myParameters = "myparam1=value1 & myparam2=value"; 

using (WebClient webClient = new WebClient()) { 
    webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 
    string HtmlResult = webClient.UploadString(URI, myParameters); 
} 
4

有一種流行的庫RestSharp

這裏是個例:

var client = new RestClient("http://example.com"); 
var request = new RestRequest("api"); 
request.AddParameter("foo", "bar"); 

client.ExecuteAsync(request, response => { 
    // do something with the response 
}); 

它轉換爲http://example.com/api?foo=bar