c# - 使用 Wordnik API 请求定义

标签 c# .net api

我只在最低限度的意义上使用过 API,所以一段时间以来我一直想尝试如何做到这一点。好的,这就是我到目前为止所拥有的并且它有效,但它返回定义的所有内容。所以我有几个问题:

  1. 有没有办法只请求定义而无需其他任何内容?
  2. 我只是解析数据吗?我在 Wordnik API 中看到,我可以包含 XML 标签...那么我可以使用 XMLReader 来获取定义吗?
  3. 现在立即请求定义以及它是否是名词/动词/等怎么样?

最终目标是创建一个我可以使用的定义列表。任何帮助将不胜感激。到目前为止,这是我的代码:

class Program
{

    static void Main(string[] args)
    {
        string apiKey = "***************";
        string wordToSearch = "";

        do
         {
            Console.Write("Please type a word to get the definition: ");
            wordToSearch = Console.ReadLine();

            if (!wordToSearch.Equals("q"))
            {
                string url = "http://api.wordnik.com/v4/word.json/" + wordToSearch + "/definitions?api_key=" + apiKey;
                WebRequest request = WebRequest.Create(url);
                request.Method = "GET";
                request.ContentType = "application/json";

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        string responseFromWordnik = reader.ReadToEnd();
                        Console.WriteLine(responseFromWordnik);
                    }
                }
            }

        } while (!wordToSearch.Equals("q"));
    }
}

谢谢, 贾斯汀

最佳答案

下面是获取单词定义的示例。您需要将 api key 替换为您自己的 api key 。

public class Word
{
    public string word { get; set; }
    public string sourceDictionary { get; set; }
    public string partOfSpeech { get; set; }
    public string text { get; set; }
}

public class WordList
{
    public List<Word> wordList { get; set; }
}


string url = "http://api.wordnik.com:80/v4/word.json/" + word + "/definitions?limit=200&includeRelated=false&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5";

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Accept = "application/json";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
webRequest.Referer = "http://developer.wordnik.com/docs.html";
webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
webRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8");
webRequest.Host = "api.wordnik.com";

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

string enc = webResponse.ContentEncoding;
using (Stream stream = webResponse.GetResponseStream())
{
    StreamReader reader = new StreamReader(stream, Encoding.UTF8);
    String responseString = "{\"wordList\":" + reader.ReadToEnd() + "}";

    if (responseString != null)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        WordList words = ser.Deserialize<WordList>(responseString);    

    }
}

关于c# - 使用 Wordnik API 请求定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124896/

相关文章:

c# - 如何从字符串中删除换行符?

api - 客户端 ID 的 Instagram 速率限制是否仅适用于未经身份验证的 API 请求?

angular - Angular 中的安全 API key (2+)

c# - 如何正确阻止异步代码?

c# - 按值(value)传递?

c# - 正则表达式逗号分隔单个数字升序排列

c# - 用于对数据进行分组的自动映射器配置

c# - 系统.BadImageFormatException : Could not load file or assembly 'x_Accessor,...' This assembly is built by a runtime newer

python - Spotify Python API 调用超时问题

c# - list<> 是否存储对属性或实例的引用?