【问题标题】:Java URL - Google Translate request returns 403 error? [duplicate]Java URL - 谷歌翻译请求返回 403 错误? [复制]
【发布时间】:2011-11-07 06:25:09
【问题描述】:

我正在制作一个 Java 控制台应用程序,它需要向 Google 翻译发送 HTTP 请求以从上述网站获取翻译。

我的问题是,当我尝试使用 openStream() 从有效 URL 读取时收到 403 错误。

例如,使用Translator t = new Translator(); 创建这个Translator 类的实例并调用t.translate("en", "ja", "cheese"); 应该会返回程序在页面http://translate.google.com/#en|ja|cheese 上找到的翻译,但它会捕获一个IOException 并返回:

http://translate.google.com/#en|ja|cheese Server returned HTTP response code: 403 for URL: @987654322@

创建有效 Google 翻译 URL 的任何其他参数都会出现类似错误。

403 error 显然意味着我被拒绝许可。这是我想知道的。为什么我无法访问此页面,我必须做什么才能访问它?

我在网络浏览器中访问了该站点,并手动输入了我的程序尝试访问的地址,但它有效;我不确定为什么我的程序无法访问该页面?将地址键入或复制/粘贴到我的 FireFox 导航栏中即可;看,如果this 是正确的,那么该站点可能希望我通过另一个页面上的链接访问该页面?如果这是我必须做的,我该如何处理那个

这是代码,我认为它可能会有所帮助.. 当我尝试从 translationURL.openStream() 返回的 InputStream 中的 InputStreamReader 创建 BufferedReader 时,似乎引发了异常:

import java.io.*;
import java.net.*;

public class Translator {

    private final String googleTranslate = "http://translate.google.com/#";

    public String translate( String from, String to, String item ) {

        String translation = googleTranslate + from + '|' + to + '|' + item;
        URL translationURL;

        try { translationURL = new URL(translation); }
        catch(MalformedURLException e) { return e.getMessage(); }

        BufferedReader httpin;
        String fullPage = "";
        System.out.println(translation);
        try {
            httpin = new BufferedReader(
                    new InputStreamReader(translationURL.openStream()));
            String line;
            while((line=httpin.readLine()) != null) { fullPage += line + '\n'; }
            httpin.close();
        } catch(IOException e) { return e.getMessage(); }

        int begin = fullPage.indexOf("<span class=\"\">");
        int end = fullPage.indexOf("</span>");

        return fullPage.substring(begin + 15, end);

    }

    public Translator() {}
}

我正在 Ubuntu Linux 11.04 上的 Eclipse (GALILEO) 中测试此代码,该代码与 Wubi 一起安装,具有工作且可靠的无线 Internet 连接。我也试过在命令行中运行它,但行为是一样的。 java -version 给我这个:

java version "1.6.0_22" OpenJDK Runtime Environment (IcedTea6 1.10.2) (6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

【问题讨论】:

标签: java http http-status-code-403


【解决方案1】:

他们正在查看用户代理字符串,大概他们不希望人们以编程方式执行此操作。

我确实让您的代码正常工作,但由于 Google 对 API 访问收费并且他们积极阻止非浏览器的东西(基于用户代理字符串),我不会告诉您我是如何做到的。

在 Java 中设置用户代理字符串的谷歌搜索会得到你想要的(事实上我在 Stackoverflow 上找到了答案)。

【讨论】:

    【解决方案2】:

    通过说我是浏览器而不是运行代码来欺骗“translate.google”。

    URLConnection conn = url.openConnection();
    // fake request coming from browser
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    

    【讨论】:

    • 这对我不起作用,这仍然有效吗?
    • @louieansonng 由于负载过大(可能有太多程序遵循此建议),Google 终止了其免费翻译 API。他们现在提供付费翻译 API,我认为价格合理且没有任何此类限制 - 请参阅 developers.google.com/translate
    【解决方案3】:

    为请求添加引用者

        URL translateURL = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) translateURL
                .openConnection();
    
        connection.setDoOutput(true);
        connection.setRequestProperty("X-HTTP-Method-Override", "GET");
        connection.setRequestProperty("referer", "accounterlive.com");
    

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 1970-01-01
      • 2022-10-24
      • 2022-07-31
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多