公共代码:
String resultURL = String.format(GOOGLE_RECOGNIZER_URL, URLEncoder.encode("hello", "UTF-8"), "en-US");
URI uri = new URI(resultURL);
byte[] resultIO = IOUtils.toByteArray(uri);
我遇到了这个例外:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.cn/translate_tts?ie=UTF-8&q=hello&tl=en-US&total=1&idx=0&textlen=3
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:654)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:635)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:617)
at com.renren.intl.soundsns.simsimi.speech.ttsclient.impl.GoogleTTSClient.main(GoogleTTSClient.java:70)
但是当我使用httpclient时,结果还可以。
String resultURL = String.format(GOOGLE_RECOGNIZER_URL, URLEncoder.encode(text, "UTF-8"), "en-US");
HttpClient client = new HttpClient();
GetMethod g = new GetMethod(resultURL);
client.executeMethod(g);
byte[] resultByte = g.getResponseBody();
这是怎么发生的?
提前感谢:)
maven依赖项:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
答案 0 :(得分:5)
Jon Skeet是对的!
对于我来说,java.net.URL JVM传递下一个标题:
User-Agent: Java/1.7.0_10
Host: translate.google.cn
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
对于Apache HttpClient:
User-Agent: Jakarta Commons-HttpClient/3.1
Host: translate.google.cn
如果更改,则为java.net.URL的用户代理:
System.setProperty("http.agent", "Jakarta Commons-HttpClient/3.1");
请求成功,没有HTTP 403。
如果您的用户代理以Java
开头,则看起来会出现403错误。任何模式为Java.*
的用户代理都会抛出403错误。但如果您使用此模式.+Java.*
一切正常。