谷歌翻译错误

发布于 2024-12-09 08:30:00 字数 1546 浏览 2 评论 0原文

我在使用 Google 翻译 API V2 时遇到异常。异常文本为“远程服务器返回错误:(403) 禁止”。调用 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;
    }

I am facing an exception while using Google translation API V2. Exception text is "The remote server returned an error: (403) Forbidden". Exception occurs when req.GetResponse()function called. I am using following code. Please mention if any correct code is available.
Thanks

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-12-16 08:30:00

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

或者

问题出在您正在使用的语言(ur = 乌尔都语?)...您应该检查这个组合是否实际上可用相应的 API。如果你真的想要西班牙语,正如你的评论所暗示的那样,我怀疑那将是es

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

You either run into some Google-set API usage limit (see http://code.google.com/apis/language/translate/v2/getting_started.html)

OR

The problem lies in the language (ur = Urdu ?) you are using... you should check whether this combination is actually available via the respective API. If you really want spanish as your comment suggests I suspect that would be es.

Another point:
You are not escaping your URL parameters (esp. the text to be translated) which in turn could lead to some problems in the future...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文