简体   繁体   中英

Is there a way to get scala code to ignore ssl validation?

I am trying to download my facebook profile image. I successully use oauth to connect and get all the right tokens, and am told by the facebook API the url for my profile image.

However when I try and retrieve it, I get an error about invalid tls. I have included some scala code showing how I download the image. I can get it if I change to http. but https fails. Can anyone suggest why? Do I need to ignore ssl validation?

I am using net.databinder.dispatch %% dispatch-core % 0.11.2 in my build.sbt

scala> import dispatch.Defaults._
scala> import dispatch._
scala> import scala.concurrent.Await
scala> import scala.concurrent.duration._

scala>   def fetchUrl( uri : String ) : Array[Byte] = {
     |     val svc = url(uri)
     |     val future_img = Http(svc OK as.Bytes)
     |     Await.result(future_img, Duration(10, "seconds"))
     |
     |     future_img()
     |   }
fetchUrl: (uri: String)Array[Byte]

scala> fetchUrl("https://scontent-syd2-1.xx.fbcdn.net/v/t1.0-1/p200x200/12347856_10153070551591710_2899469599397194349_n.jpg?_nc_cat=105&_nc_sid=dbb9e7&_nc_ohc=1wqEuhGf4S4AX_iqRx5&_nc_ht=scontent-syd2-1.xx&tp=6&oh=4a792c1f0f4eded4ff2516efdfb056ab&oe=5F8D2DC4")

throws an exceeption about an invalid ss/tls record

21:08:00.295 [New I/O worker #1] DEBUG c.n.h.c.p.n.NettyAsyncHttpProvider - Unexpected I/O exception on channel [id: 0xa80fd837, /10.0.0.16:59254 => scontent-syd2-1.xx.fbcdn.net/157.240.8.23:443]
org.jboss.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 

If I change the https to http then it works. Also if I paste the URI in to the browser address bar it works as https and reports the cert is valid.

scala> fetchUrl("http://scontent-syd2-1.xx.fbcdn.net/v/t1.0-1/p200x200/12347856_10153070551591710_2899469599397194349_n.
jpg?_nc_cat=105&_nc_sid=dbb9e7&_nc_ohc=1wqEuhGf4S4AX_iqRx5&_nc_ht=scontent-syd2-1.xx&tp=6&oh=4a792c1f0f4eded4ff2516efdfb
056ab&oe=5F8D2DC4")

scala> fetchUrl("http://scontent-syd2-1.xx.fbcdn.net/v/t1.0-1/p200x200/12347856_10153070551591710_2899469599397194349_n.
jpg?_nc_cat=105&_nc_sid=dbb9e7&_nc_ohc=1wqEuhGf4S4AX_iqRx5&_nc_ht=scontent-syd2-1.xx&tp=6&oh=4a792c1f0f4eded4ff2516efdfb
056ab&oe=5F8D2DC4")

056ab&oe=5F8D221:08:53.263 [New I/O worker #3] DEBUG c.n.h.c.p.netty.NettyConnectionsPool - Adding uri: http://scontent-syd2-1.xx.fbcdn.net:80 for channel [id: 0xa499e1d5, /10.0.0.16:59341 => scontent-syd2-1.xx.fbcdn.net/157.240.8.23:80]
res1: Array[Byte] = Array(-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -19, 0, -100, 80, 104, 111, 116, 111, 115, 104, 111, 112, 32, 51, 46, 48, 0, 56, 66, 73, 77, 4, 4, 0, 0, 0, 0, 0, -128, 28, 2, 103, 0, 20, 107, 111, 56, 50, 105, 103, 81, 77, 53, 57, 85, 55, 103, 73, 72, 45, 110, 119, 106, 75, 28, 2, 40, 0, 98, 70, 66, 77, 68, 48, 97, 48, 48, 48, 97, 56, 56, 48, 49, 48, 48, 48, 48, 99, 55, 48, 52, 48, 48, 48, 48, 102, 52, 48, 97, 48, 48, 48, 48, 49, 98, 48, 99, 48, 48, 48, 48, 53, 102, 48, 100, 48, 48, 48, 48, 57, 99, 49, 52, 48, 48, 48, 48, 99, 56, 49, 100, 48, 48, 48, 48, 55, 57, 49, 101, 48, 48, 48, 48, 98, 101, 49, 102, 48, 48, 48, 48, 49, 49, 50, 49, 48, 48, 48, 48, 54, 97, 51, 48, 48, 48, 48, 48, -1, -37, 0, 67, 0, 6, 4, 5, 6, 5, 4, 6, 6, 5, 6, 7, ...

My build.sbt was also bringing in 0.8.10 of dispatch-core. I am still having issues but will post another question. Removing the dependency on 0.8.10 resolved the ssl issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM