2
votes

I have this simple piece of code:

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()
  }
}

Basically I'm using Play! WS utils to get the http response code from a url, the problem is that this url has a temporary redirect (it returns 307) and when redirecting the url appears not to be encoded, this is the message printed from the catch clause:

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

I also tried other types of encoding(LATIN1, some ISOs), am I doing something wrong or is it a problem in the redirect checking from Play! web services?

As noted by wingedsubmariner the lp-loaded-variation-Dᅢᄊhren is being returned as part of a Set-Cookie header.

1
The lp-loaded-variation-Dᅢᄊhren is actually being returned as part of a Set-Cookie header. I have no idea why Play! decides to choke on this. - wingedsubmariner
Thanks for pointing that out. - Ende Neu

1 Answers

0
votes

May not help if you are stuck with Play 2.2 but this seems to work in Play 2.3.6. Add "com.typesafe.play" %% "play-ws" % "2.3.6" to 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")
      }
  }

}