谷歌翻译错误
我在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要么遇到了 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 bees
.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...