Java URL

编程入门 行业动态 更新时间:2024-10-12 08:22:36
本文介绍了Java URL - 谷歌翻译请求返回403错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm making a Java console application that needs to send an HTTP request to Google Translate to get a translation from the aforementioned site.

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

My problem, is that I receive a 403 error when I try to read from a valid URL, using openStream().

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

Creating an instance of this Translator class with Translator t = new Translator(); and calling t.translate("en", "ja", "cheese");, for example, should return the translation the program finds on the page translate.google/#en|ja|cheese, it seems, but instead it catches an IOException and returns this:

translate.google/#en|ja|cheese 服务器返回HTTP响应代码:403为网址: translate.google/#en|ja|cheche

任何ot都会发生类似的错误她的论点是创建有效的Google翻译网址。

A similar error occurs with any other arguments that create a valid Google Translate URL.

A 403错误显然意味着我被拒绝许可。这就是我想知道的。为什么我不能访问此页面,以及我必须做什么才能访问它?

A 403 error apparently means I am denied permission. This is what I want to know about. Why can't I access this page, and what must I do in order to access it?

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

I've visited the site in my web browser, and entered the address that my program tries to access, manually, but it worked; I'm not sure why my program thus cannot access the page? Typing or copy/pasting the address into my FireFox navigation bar works; see, if this is correct, then the site may be wanting me to access the page via links on another page? How might I go about that, if that's what I must do?

这是代码,因为我觉得它可能会有所帮助..例外当我尝试从InputStream中的InputStreamReader创建一个BufferedReader时,似乎抛出了 translationURL.openStream():

Here's the code, as I think it may help.. the exception seems to be thrown when I try to create a BufferedReader from an InputStreamReader from the InputStream returned by translationURL.openStream():

import java.io.*; import java.*; public class Translator { private final String googleTranslate = "translate.google/#"; 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 让我这样:

I am testing this code in Eclipse (GALILEO) on Ubuntu Linux 11.04, installed with Wubi, with a working and reliable wireless Internet connection. I've also tried running it in the command line, but the behavior was the same. java -version got me this:

java version1.6.0_22 OpenJDK运行时环境(IcedTea6 1.10.2)(6b22-1.10.2-0ubuntu1~11.04.1) OpenJDK 64位服务器VM(版本20.0-b11,混合模式)

推荐答案

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

They are looking at the user agent string, and presumably they don't want people doing this programatically.

我确实让您的代码正常工作,但由于Google收取API访问费用,并且他们主动阻止不是浏览器的内容(基于用户代理字符串)我不会告诉你我是怎么做到的。

I did get your code working working, but since Google charges for the API access and they are actively blocking things that are not browsers (based on the user agent string) I won't tell you how I did it.

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

A google search for setting the user agent string in Java will get you what you want (as a matter of fact I found the answer here on Stackoverflow).

更多推荐

Java URL

本文发布于:2023-10-11 14:30:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!