我有这段简单的代码:
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从url获取http响应代码,问题是这个url有一个临时重定向(它返回307),当重定向url似乎不是编码,这是从catch子句打印的消息:
name contains non-ascii character: lp-loaded-variation-Dᅢᄊhren
我还尝试过其他类型的编码(LATIN1
,某些ISO
),我做错了什么,或者是从Play!
网络服务进行重定向检查时出现问题?< / p>
如wingedsubmariner所述,lp-loaded-variation-Dᅢᄊhren
作为Set-Cookie标头的一部分返回。
答案 0 :(得分:0)
如果您遇到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")
}
}
}