403尝试下载远程图像时

Seb*_*ber 6 java scala playframework-2.0

我想从一些网址下载图片.对于一些图片它工作正常,但对于其他人我得到403错误.

例如,这个:http://blog.zenika.com/themes/Zenika/img/zenika.gif

此图片访问不需要任何身份验证.您可以单击链接上的自己,并使用200状态代码验证它是否可用于您的浏览器.

以下代码生成异常:new java.net.URL(url).openStream().同样的,在引擎盖下org.apache.commons.io.FileUtils.copyURLToFile(new java.net.URL(url), tmp)使用相同的openStream()方法.

java.io.IOException: Server returned HTTP response code: 403 for URL: http://blog.zenika.com/themes/Zenika/img/zenika.gif
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626) ~[na:1.7.0_45]
at java.net.URL.openStream(URL.java:1037) ~[na:1.7.0_45]
at services.impl.DefaultStampleServiceComponent$RemoteImgUrlFilter$class.downloadAsTemporaryFile(DefaultStampleServiceComponent.scala:548) [classes/:na]
at services.impl.DefaultStampleServiceComponent$RemoteImgUrlFilter$class.services$impl$DefaultStampleServiceComponent$RemoteImgUrlFilter$$handleImageUrl(DefaultStampleServiceComponent.scala:523) [classes/:na]
Run Code Online (Sandbox Code Playgroud)

我使用Scala/Play Framework开发.我试图使用内置的AsyncHttpClient.

// TODO it could be better to use itetarees on the GET call becase I think AHC load the whole body in memory
WS.url(url).get.flatMap { res =>
  if (res.status >= 200 && res.status < 300) {
    val bodyStream = res.getAHCResponse.getResponseBodyAsStream
    val futureFile = TryUtils.tryToFuture(createTemporaryFile(bodyStream))
    play.api.Logger.info(s"Successfully downloaded file $filename with status code ${res.status}")
    futureFile
  } else {
    Future.failed(new RuntimeException(s"Download of file $filename returned status code ${res.status}"))
  }
} recover {
  case NonFatal(e) => throw new RuntimeException(s"Could not downloadAsTemporaryFile url=$url", e)
}
Run Code Online (Sandbox Code Playgroud)

有了这个AHC代码,它工作正常.有人可以解释这种行为,为什么我的URL.openStream()方法有403错误?

oue*_*ani 6

如前所述,一些主机使用一些像 UserAgent 这样的标头来防止这种入侵:

这不起作用:

   val urls = """http://blog.zenika.com/themes/Zenika/img/zenika.gif"""
  val url = new URL(urls)
  val urlConnection = url.openConnection() 
  val inputStream = urlConnection.getInputStream()
  val bufferedReader = new BufferedReader(new InputStreamReader(inputStream))
Run Code Online (Sandbox Code Playgroud)

这有效:

val urls = """http://blog.zenika.com/themes/Zenika/img/zenika.gif"""
val url = new URL(urls)
val urlConnection = url.openConnection()   
urlConnection.setRequestProperty("User-Agent", """NING/1.0""") 
val inputStream = urlConnection.getInputStream()
val bufferedReader = new BufferedReader(new InputStreamReader(inputStream))
Run Code Online (Sandbox Code Playgroud)