Skip to content

Instantly share code, notes, and snippets.

@ruescasd
Created January 27, 2017 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruescasd/b49e869548d1f7c8731797d37702bce6 to your computer and use it in GitHub Desktop.
Save ruescasd/b49e869548d1f7c8731797d37702bce6 to your computer and use it in GitHub Desktop.
import java.io.File
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.WSClient
import play.api.libs.ws.WSConfigParser
import scala.concurrent.Future
import scala.util.{Success, Failure}
import play.api.{Environment, Mode}
import play.api.Configuration
import play.api.libs.ws.ahc.{AhcConfigBuilder, AhcWSClient, AhcWSClientConfig}
object WebRequest {
import scala.concurrent.ExecutionContext.Implicits._
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
try {
System.setProperty("javax.net.ssl.trustStore", "")
System.setProperty("javax.net.debug", "ssl")
val environment = Environment(new File("."), this.getClass.getClassLoader, Mode.Dev)
val configuration = Configuration.load(environment)
val parser = new WSConfigParser(configuration, environment)
val config = AhcWSClientConfig(wsClientConfig = parser.parse())
val builder = new AhcConfigBuilder(config)
val ahcBuilder = builder.configure()
val ahcConfig = ahcBuilder.build()
val wsClient = new AhcWSClient(ahcConfig)(materializer)
call(wsClient)
.andThen {
case Success(x) => println(x)
case Failure(e) => e.printStackTrace()
}
.andThen { case _ => wsClient.close() }
.andThen { case _ => system.terminate() }
}
catch {
case e: Throwable => e.printStackTrace(); system.terminate()
}
system.terminate()
}
def call(wsClient: WSClient): Future[String] = {
wsClient.url("https://localhost/web/hello").get().map { response =>
val statusText: String = response.statusText
response.body
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment