scala http get
时间: 2023-09-10 07:10:50 浏览: 46
可以使用 Scala 内置的 Http 包或者第三方库如 Akka Http 来发送 Http GET 请求。以下是使用 Scala Http 包发送 Http GET 请求的示例代码:
```scala
import scala.io.Source
import java.net.URL
def httpGet(url: String): String = {
val con = new URL(url).openConnection()
con.setRequestProperty("User-Agent", "Mozilla/5.0") // 设置 User-Agent
Source.fromInputStream(con.getInputStream).mkString
}
val result = httpGet("https://www.baidu.com")
println(result)
```
这段代码将发送一个 GET 请求到百度首页,并打印返回的页面内容。需要注意的是,需要设置 User-Agent 请求头,否则有些网站可能会拒绝请求。
相关问题
scala 提供 http get put 数据服务接口 server端
以下是使用 Scala 提供 HTTP GET 和 PUT 数据服务接口的示例:
GET 请求:
```scala
import java.net.InetSocketAddress
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
object HttpServerDemo {
def main(args: Array[String]): Unit = {
val server = HttpServer.create(new InetSocketAddress(8080), 0)
server.createContext("/data", new DataHandler())
server.setExecutor(null)
server.start()
}
}
class DataHandler extends HttpHandler {
override def handle(exchange: HttpExchange): Unit = {
val response = "This is a GET request."
exchange.sendResponseHeaders(200, response.length)
val os = exchange.getResponseBody
os.write(response.getBytes)
os.close()
}
}
```
PUT 请求:
```scala
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStream
import java.net.InetSocketAddress
import com.sun.net.httpserver.{HttpExchange, HttpHandler, HttpServer}
object HttpServerDemo {
def main(args: Array[String]): Unit = {
val server = HttpServer.create(new InetSocketAddress(8080), 0)
server.createContext("/data", new DataHandler())
server.setExecutor(null)
server.start()
}
}
class DataHandler extends HttpHandler {
override def handle(exchange: HttpExchange): Unit = {
if (exchange.getRequestMethod.equalsIgnoreCase("PUT")) {
val is = exchange.getRequestBody
val br = new BufferedReader(new InputStreamReader(is))
var line = ""
var result = ""
while (line != null) {
line = br.readLine()
if (line != null) {
result += line
}
}
is.close()
val response = "This is a PUT request with data: " + result
exchange.sendResponseHeaders(200, response.length)
val os = exchange.getResponseBody
os.write(response.getBytes)
os.close()
} else {
val response = "Only PUT requests are allowed."
exchange.sendResponseHeaders(405, response.length)
val os = exchange.getResponseBody
os.write(response.getBytes)
os.close()
}
}
}
```
scala读取influxdb
Scala可以使用InfluxDB的Java客户端库来读取InfluxDB数据。首先,你需要在你的项目中添加InfluxDB的依赖。可以在build.sbt文件中添加以下内容:
```scala
libraryDependencies += "org.influxdb" % "influxdb-java" % "2.17"
```
然后,你可以使用以下代码示例来读取InfluxDB中的数据:
```scala
import org.influxdb.InfluxDBFactory
import org.influxdb.dto.Query
import scala.collection.JavaConverters._
val influxDB = InfluxDBFactory.connect("http://localhost:8086", "username", "password")
val query = new Query("SELECT * FROM measurement", "database")
val result = influxDB.query(query)
val points = result.getResults.asScala.flatMap(_.getSeries.asScala.flatMap(_.getValues.asScala))
points.foreach { point =>
val time = point.get(0).asInstanceOf[String]
val value = point.get(1).asInstanceOf[Double]
println(s"$time: $value")
}
influxDB.close()
```
在上面的代码中,你需要替换`http://localhost:8086`为你的InfluxDB的URL,以及`username`和`password`为你的InfluxDB的用户名和密码。同时,你还需要将`measurement`替换为你要查询的测量值名称,`database`替换为你要查询的数据库名称。
这段代码首先连接到InfluxDB,并执行给定的查询。然后,它将结果转换为Scala集合,并遍历每个数据点,打印时间和值。最后,它关闭InfluxDB连接。
希望对你有所帮助!如果你有任何问题,请随时提问。