【问题标题】:How can I request an Image from a URL when the Response is 302?当响应为 302 时,如何从 URL 请求图像?
【发布时间】:2014-08-05 00:24:46
【问题描述】:

我想从我的 Grails 服务方法中的 url 请求图像。当我从 url(在本例中来自 Facebook)请求图像时,我得到 302 重定向。之后我再次请求新的 URL。在第二个响应中,我得到了 403 禁止。

这是我使用的 Service 方法:

        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 时如何从 url 请求图片?

编辑:

以下是 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。

【问题讨论】:

  • 您向 302 响应 Location 标头中的 URL 发送新请求。如果这需要身份验证,则进行身份验证。

标签: java spring spring-mvc grails groovy


【解决方案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()
     }
  }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
相关资源
最近更新 更多