HTTP URL Address Encoding in Scala/Java

Viewed 905

I want a function that loads the contents of a url address, but I do not know in advance if the url is encoded or not. Is there a library function that solves that?

One possible soluction would be the decode the url first and then encode it, but I would have to treat the each part of the url in a different way (in the example bellow the no ASCII character is in the path part but it could also be in the query part). There are so many details to handle, one library that treats that would be wonderfull.


object UrlContent extends App {
  def connect(urls: String): Int = {
    val url = new URL(urls)
    val conn: HttpURLConnection = url.openConnection().asInstanceOf[HttpURLConnection]

    conn.getResponseCode
  }

  val urls1 = "http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzo%C3%B1osos.pdf"
  val urls2 = "http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzoñosos.pdf"

  println(connect(urls1))
  println(connect(urls2))
}```

The output is:

200
404
1 Answers

Try lemonlabsuk/scala-uri, for example

import io.lemonlabs.uri.Url

val urls1 = Url.parse("http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzo%C3%B1osos.pdf")
val urls2 = Url.parse("http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzoñosos.pdf")

println(urls1)
println(urls2)

outputs in both cases

http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzo%C3%B1osos.pdf
http://www.ins.gob.pe/insvirtual/images/otrpubs/pdf/ponzo%C3%B1osos.pdf

so it seems it is able to detect if the URL is already encoded.

Related