如何在C#中编写REST Get请求?

3

我是C#的新手,还在努力熟悉它的环境。

我想以Get方式进行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,但是我不知道如何将我的请求与参数一起使用。


你能展示一下你的WebClient方法吗?这样我们就可以看到它有什么问题了。 - coolmine
使用谷歌搜索一个REST库。维基百科上有一篇关于REST的论文链接,阅读起来很有帮助。 - mrsteve
2个回答

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进行翻译。


2

试试这个

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接