【问题标题】:Play! Web services: redirect with encoded URL玩! Web 服务:使用编码的 URL 重定向
【发布时间】:2014-11-26 13:10:30
【问题描述】:

我有这段简单的代码:

object Main {

  def main(args: Array[String]) {
    val application = new DefaultApplication(new File(args(0)), this.getClass.getClassLoader, null, Mode.Prod)
    Play.start(application)

    val test: Future[Int] = WS.url("http://www.bestattungsvergleich.de/go/" + URLEncoder.encode("Döhren", "UTF-8"))
      .withFollowRedirects(true)
      .withRequestTimeout(5000)
      .get()
      .map(x => {
        println(x.status)
        x.status
      })
      .recover { case e: Exception => {
        println(e.getMessage)
        1000
      }
    }
    println(Await.result(test, Duration.Inf))
    Play.stop()
  }
}

基本上我使用Play! WS utils 从一个u​​rl 获取http 响应代码,问题是这个url 有一个临时重定向(它返回307)并且重定向时url 似乎没有被编码,这是从 catch 子句打印的消息:

name contains non-ascii character: lp-loaded-variation-Dᅢᄊhren

我还尝试了其他类型的编码(LATIN1,一些ISOs),是我做错了什么还是来自Play! Web 服务的重定向检查有问题?

正如wingedsubmariner 所述,lp-loaded-variation-Dᅢᄊhren 将作为 Set-Cookie 标头的一部分返回。

【问题讨论】:

标签: scala http playframework playframework-2.0


【解决方案1】:

如果您被 Play 2.2 卡住,可能无济于事,但这似乎在 Play 2.3.6 中有效。将 "com.typesafe.play" %% "play-ws" % "2.3.6" 添加到 build.sbt

import java.net.URLEncoder
import play.api.libs.ws.ning._
import play.api.libs.ws._

object testing extends App {

  override def main(args: Array[String]) {
    implicit val context = play.api.libs.concurrent.Execution.Implicits.defaultContext
    val config = new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build()
    val builder = new com.ning.http.client.AsyncHttpClientConfig.Builder(config)
    val wsClient: NingWSClient = new NingWSClient(builder.build())
    val result = wsClient.url("http://www.bestattungsvergleich.de/go/" + URLEncoder.encode("Döhren", "UTF-8"))
      .withRequestTimeout(3000)
      .withFollowRedirects(true)
      .get
      .map { response =>
        println("Got a response")
        println(response.body)
        // close out properly in real application not like this
        wsClient.underlying[com.ning.http.client.AsyncHttpClient].close;
        System.exit(0)
      }
      .recover {
        case e: Throwable =>
          println("error", e)
          println("Couldn't open")
      }
  }

}

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 2015-01-26
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多