当响应为302时,如何从URL请求图像?

时间:2014-08-05 00:24:46

标签: java spring spring-mvc grails groovy

我想从Grails Service方法中请求来自网址的图片。当我从网址(在这种情况下来自Facebook)请求图像时,我得到302重定向。之后,我再次请求新的URL。在第二个回复中,我得到403被禁止。

以下是我使用的服务方法:

        static queryImageUrl(url, query, MediaType contentType = urlencodedMediaType) {
                if(query instanceof Map) {
                        query = map2query query
                }

                def uri = new URI("${url}?$query")
                def requestFactory = new SimpleClientHttpRequestFactory()

                def request = requestFactory.createRequest(uri, HttpMethod.GET)

                try {
                        def response = request.execute()
                        def statusCode = response.statusCode
                        log.debug "got reply from $uri with status code $statusCode"
                        if (statusCode == HttpStatus.FOUND) {
                                def newUrl = response.connection.responses.getHeaders().Location[0]
                                log.debug "302 redirect to ${newUrl}"
                                return queryImageUrl(newUrl, "")
                        }

                        if (statusCode == HttpStatus.OK) {
                                return response.body.bytes
                        }
                        else if(statusCode == HttpStatus.FORBIDDEN) {
                                throw new IllegalAccessError(response.statusCode.toString())
                        }
                } catch(ex) {
                        log.error "Exception while querying $url", ex
                }



                return null

        }

当响应状态为302时,如何从网址请求图片?

修改:

以下是来自Facebook用户的图片请求的日志:

got reply from http://graph.facebook.com/10002342342395/picture?width=200&height=200 with status code 302

302 redirect to https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/10491179_578345345924700_6665003172531587407_n.jpg?oh=d91657acaf300c3d01042eccc22cf006&oe=543C6E19&__gda__=1415182364_3afb5be4b59d61c4bb2994d2605d2c65

got reply from https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/v/t1.0-1/p200x200/10491179_578563345345924700_666503453451587407_n.jpg?oh=d91657acaf300c3d01042eccc22cf006&oe=543C6E19&__gda__=1415182364_3afb5be4b59d61c4bb2994d2605d2c65? with status code 403

我的递归方法在第一次响应后第二次被调用,因为302而第二次响应报告403。

1 个答案:

答案 0 :(得分:1)

最近有完全相同的问题。然后发现,HTTPBuilder正确处理重定向:

  ByteArrayOutputStream baos = new ByteArrayOutputStream()
  new HTTPBuilder( picUrl ).get( contentType:ContentType.BINARY ){ resp, reader ->
    baos << reader 
  }
  persistByteArraySomehow baos.toByteArray()

更新

  static queryImageUrl(url, query, MediaType contentType = urlencodedMediaType) {
     if(query instanceof Map) {
        query = map2query query
     }

     def uri = "${url}?$query"
     ByteArrayOutputStream baos = new ByteArrayOutputStream()
     try{
       new HTTPBuilder( uri ).get( contentType:ContentType.BINARY ){ resp, reader ->
         baos << reader 
       }
       baos.toByteArray()
     }finally{
       baos.close()
     }
  }