要使用google translate api,我想出了这个网址
如果单击它,您将获得一个json格式的文件,并具有准确的翻译。 要使用java程序检索此文件,我编写了以下代码。
String word = "revenge";
System.setProperty("http.proxyHost", "172.30.0.16");
System.setProperty("http.proxyPort", "3128");
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();
但它显示以下错误
线程“main”中的异常java.io.IOException:服务器返回HTTP响应代码:403为URL:http://translate.google.com/translate_a/t?client=t&text=Moon&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1403) 在java.net.URL.openStream(URL.java:1029) 在smsMain.main(smsMain.java:20
希望得到一些帮助。因为我们手动获取文件但是使用程序是禁止的。
答案 0 :(得分:0)