【问题标题】:Google translation error谷歌翻译错误
【发布时间】:2011-10-13 07:42:03
【问题描述】:

我在使用 Google 翻译 API V2 时遇到异常。异常文本是“远程服务器返回错误:(403) Forbidden”。调用 req.GetResponse() 函数时发生异常。我正在使用以下代码。请提及是否有任何正确的代码可用。 谢谢

public static string Translate()
    {
         String textToTranslate = "Common";
         String fromLanguage = "en"; // english
         String toLanguage = "ur"; // spanish
         String apiKey = /*My API Key*/; 

        // create the url for making web request
         String apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
         String url = String.Format(apiUrl, apiKey, fromLanguage, toLanguage, textToTranslate);    
         string text = string.Empty;

        try
        {
            // create the web request
            WebRequest req = HttpWebRequest.Create(url);

            // set the request method
            req.Method = "GET";

            // get the response
            using (WebResponse res = req.GetResponse())
            {
                // read response stream
                // you must specify the encoding as UTF8 
                // because google returns the response in UTF8 format
                using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
                {
                    // read text from response stream
                    text = sr.ReadToEnd();
                }
            }
        }
        catch (Exception e)
        {
            throw; // throw the exception as is/
        }

        // return text to callee
        return text;
    }

【问题讨论】:

    标签: c# google-translate


    【解决方案1】:

    您遇到了一些 Google 设置的 API 使用限制(请参阅 http://code.google.com/apis/language/translate/v2/getting_started.html

    问题在于您使用的语言(ur = Urdu ?)...您应该检查此组合是否可以通过相应的 API 实际使用。如果您真的想要西班牙语,正如您的评论所暗示的那样,我怀疑那将是es

    还有一点:
    您没有转义您的 URL 参数(尤其是要翻译的文本),这反过来可能会导致将来出现一些问题...

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      相关资源
      最近更新 更多