公共 io 代码:
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>