Why does dispatch throw “java.net.ConnectException: General SSLEngine …” and “unexpected status” exceptions for a particular URL?

≡放荡痞女 提交于 2019-12-02 10:59:24

问题


I have the following non-working code:

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

I would like to know why "s1" works fine, whereas "s2" and "s3" throw exceptions. The exceptions thrown are:

Exception 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) 

Exception 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)

Also, all three URLs work as expected when I access them through the Safari web browser. Why does the first URL work fine through dispatch, but the last two don't?


回答1:


If you want to trust all certificates, as in the linked Play example, then try this:

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



回答2:


And to create an Http client that does verify the certificates, I found some sample code here: https://kevinlocke.name/bits/2012/10/03/ssl-certificate-verification-in-dispatch-and-asynchttpclient/.



来源:https://stackoverflow.com/questions/46301238/why-does-dispatch-throw-java-net-connectexception-general-sslengine-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!