在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:scalaj/scalaj-http开源软件地址:https://github.com/scalaj/scalaj-http开源编程语言:Scala 88.4%开源软件介绍:THIS LIBRARY IS DEPRECATEDAs of April 2022 there are many supported options for making HTTP calls in scala:
If you've inherited a codebase that's using this library, I would suggest just copying the source code (it's only four files, and no depedencies) and make whatever fixes/changes you want. Good luck! Simplified HttpThis is a fully featured http client for Scala which wraps java.net.HttpURLConnection Features:
Non-Features:
Works in Google AppEngine and Android environments. Big differences:
Installationin your build.sbtlibraryDependencies += "org.scalaj" %% "scalaj-http" % "2.4.2" maven<dependency>
<groupId>org.scalaj</groupId>
<artifactId>scalaj-http_${scala.version}</artifactId>
<version>2.4.2</version>
</dependency> If you're including this in some other public library. Do your users a favor and change the fully qualified name so they don't have version conflicts if they're using a different version of this library. The easiest way to do that is just to copy the source into your project :) UsageSimple Getimport scalaj.http._
val response: HttpResponse[String] = Http("http://foo.com/search").param("q","monkeys").asString
response.body
response.code
response.headers
response.cookies Immutable Request
val request: HttpRequest = Http("http://date.jsontest.com/")
val responseOne = request.asString
val responseTwo = request.asString Additive RequestAll the "modification" methods of a Simple form encoded POSTHttp("http://foo.com/add").postForm(Seq("name" -> "jon", "age" -> "29")).asString OAuth v1 Dance and RequestNote: the import scalaj.http.{Http, Token}
val consumer = Token("key", "secret")
val response = Http("https://api.twitter.com/oauth/request_token").postForm(Seq("oauth_callback" -> "oob"))
.oauth(consumer).asToken
println("Go to https://api.twitter.com/oauth/authorize?oauth_token=" + response.body.key)
val verifier = Console.readLine("Enter verifier: ").trim
val accessToken = Http("https://api.twitter.com/oauth/access_token").postForm.
.oauth(consumer, response.body, verifier).asToken
println(Http("https://api.twitter.com/1.1/account/settings.json").oauth(consumer, accessToken.body).asString) Parsing the responseHttp("http://foo.com").{asString, asBytes, asParams} Those methods will return an Advanced Usage ExamplesParse the response InputStream directlyval response: HttpResponse[Map[String,String]] = Http("http://foo.com").execute(parser = {inputStream =>
Json.parse[Map[String,String]](inputStream)
}) Post raw Array[Byte] or String data and get response codeHttp(url).postData(data).header("content-type", "application/json").asString.code Post multipart/form-dataHttp(url).postMulti(MultiPart("photo", "headshot.png", "image/png", fileBytes)).asString You can also stream uploads and get a callback on progress: Http(url).postMulti(MultiPart("photo", "headshot.png", "image/png", inputStream, bytesInStream,
lenWritten => {
println(s"Wrote $lenWritten bytes out of $bytesInStream total for headshot.png")
})).asString Stream a chunked transfer response (like an event stream)Http("http://httpbin.org/stream/20").execute(is => {
scala.io.Source.fromInputStream(is).getLines().foreach(println)
}) note that you may have to wrap in a while loop and set a long readTimeout to stay connected Send https request to site with self-signed or otherwise shady certificateHttp("https://localhost/").option(HttpOptions.allowUnsafeSSL).asString Do a HEAD requestHttp(url).method("HEAD").asString Custom connect and read timeoutsThese are set to 1000 and 5000 milliseconds respectively by default Http(url).timeout(connTimeoutMs = 1000, readTimeoutMs = 5000).asString Get request via a proxyval response = Http(url).proxy(proxyHost, proxyPort).asString Other custom optionsThe Change the CharsetBy default, the charset for all param encoding and string response parsing is UTF-8. You can override with charset of your choice: Http(url).charset("ISO-8859-1").asString Create your own HttpRequest builderYou don't have to use the default Http singleton. Create your own: object MyHttp extends BaseHttp (
proxyConfig = None,
options = HttpConstants.defaultOptions,
charset = HttpConstants.utf8,
sendBufferSize = 4096,
userAgent = "scalaj-http/1.0",
compress = true
) Full API documentationDealing with annoying java library issues
Overriding the |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论