2012-09-03 48 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