我想在java中使用此链接下载的文件...
http://translate.google.com/translate_a/ ...
如果单击上面的链接,您将获得一个json格式的文件。我想使用java代码获取此文件。
我试过这个
String word = "Moon";
System.setProperty("http.proxyHost", "172.20.0.100");
System.setProperty("http.proxyPort", "8080");
URL url = new URL("http://translate.google.com/translate_a/t?client=t&text="+word+"&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1");
url.openConnection();
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("t");
byte[] buffer = new byte[153600];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
}
writer.close();
reader.close();
错误
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.com/translate_a/…
at sun.net.www.protocol.http.HttpURLConnect…
at java.net.URL.openStream(URL.java:1031)
at smsMain.main(smsMain.java:19)
直接下载正在运行(只需点击并查看印地文中“月亮”一词的翻译)但通过程序显示错误!为什么???
请帮助这方面。 提前谢谢!!!
答案 0 :(得分:4)
您收到的是403,因为Google不希望您以这种方式使用该服务:http://en.wikipedia.org/wiki/HTTP_403
如果您查看页面内容,则会显示:That’s an error. Your client does not have permission to get URL / from this server.
要获得对Google Translate API的编程访问权限,您需要为访问付费https://developers.google.com/translate/