为什么dispatch会抛出“java.net.ConnectException:General SSLEngine ...”和特定URL的“意外状态”异常?

时间:2017-09-19 13:02:45

标签: java scala http web scala-dispatch

我有以下非工作代码:

object Main extends App {
  import dispatch._

  def test(address: String) = {
    Await.result(Http.default(url(address).GET OK as.String), Duration.Inf)
  }

  // This works fine
  val s1 = test("http://download.finance.yahoo.com/d/quotes.csv?s=MSFT&f=sohgbav")
  println(s1)

  // This throws Exception 1
  val s2 = test("http://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv")
  println(s2) 

  // This throws Exception 2
  val s3 = test("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv")
  println(s3) 
}

我想知道为什么“s1”工作正常,而“s2”和“s3”则抛出异常。抛出的异常是:

例外1:

[error]   ! access URL
[error]    java.util.concurrent.ExecutionException: dispatch.StatusCode: Unexpected response status: 301 (NettyResponseFuture.java:172)
[error] org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:172)
[error] dispatch.HttpExecutor.$anonfun$apply$3(execution.scala:123) 

例外2:

[error]   ! access URL
[error]    java.util.concurrent.ExecutionException: java.net.ConnectException: General SSLEngine problem (NettyResponseFuture.java:172)
[error] org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:172)
[error] dispatch.HttpExecutor.$anonfun$apply$3(execution.scala:123)

此外,当我通过Safari Web浏览器访问它们时,所有三个URL都按预期工作。为什么第一个URL通过发送工作正常,但最后两个没有?

2 个答案:

答案 0 :(得分:1)

如果您想信任所有证书,例如在链接的Play示例中,请尝试以下操作:

Http.withConfiguration(config => config.setAcceptAnyCertificate(true))(url(address).GET OK as.String)

答案 1 :(得分:0)

要创建一个验证证书的Http客户端,我在这里找到了一些示例代码:https://kevinlocke.name/bits/2012/10/03/ssl-certificate-verification-in-dispatch-and-asynchttpclient/