commons io 403 for URL但是httpclient还可以 [英] commons io 403 for URL but httpclient is ok

查看:172
本文介绍了commons io 403 for URL但是httpclient还可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

commons io code:

commons io code :

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时,结果还可以。

but when I use httpclient, the result is ok.

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>


推荐答案

Jon Skeet是对的!

Jon Skeet is right!

对于我来说,如果是java.net.URL,JVM传递下一个标题:

For me in case of java.net.URL JVM pass next headers:

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:

In case of Apache HttpClient:

User-Agent: Jakarta Commons-HttpClient/3.1
Host: translate.google.cn

如果你改变了,java.net.URL的用户代理:

And if you change, the user agent for java.net.URL:

System.setProperty("http.agent", "Jakarta Commons-HttpClient/3.1");

请求成功,没有HTTP 403。

request is successful, without HTTP 403.

如果您的用户代理以: Java 开头,看起来会出现403错误。任何模式为 Java。* 的用户代理都会抛出403错误。但如果您使用此模式。+ Java。* 一切正常。

Looks like you get 403 error if your user-agent start with: Java. Any user agent with pattern Java.* throws 403 error. But if you use this pattern .+Java.* all is ok.

这篇关于commons io 403 for URL但是httpclient还可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆