Scala http 操作
如何在 Scala 中执行以下操作?
- HTTP Get
- 使用自定义标头的 HTTP Get
- HTTP Post
How do I perform the following in Scala?
- HTTP Get
- HTTP Get With custom headers
- HTTP Post
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以尝试Dispatch。一开始有点难以掌握,但一段时间后我开始喜欢它。它在 HttpClient 之上工作。
You could try out Dispatch. A little difficult to grasp at first, but after a while I've started to like it. It works on top of HttpClient.
您可以简单地使用 java.net.URL 发送 HTTP GET 和 HTTP POST 请求。
您还可以在 HttpURLConnection 上设置 HTTP 请求标头,如下所示:
我自己编写了一个实用程序类,它正是执行此操作。您可以在这里看到它:
https://github.com/gruenewa/gruenewa-misc/blob/master/gruenewa-wsclient/src/main/scala/gruenewa/wsclient/Service.scala
You can simply use java.net.URL to send HTTP GET and HTTP POST requests.
You can also set HTTP request headers on the HttpURLConnection like this:
I have written myself a utility class which does exactly this. You can see it here:
https://github.com/gruenewa/gruenewa-misc/blob/master/gruenewa-wsclient/src/main/scala/gruenewa/wsclient/Service.scala
这是我自己实现的一个简单的 Http 客户端,包括 cookie 管理。也许它对你有用。但我不确定是否可以直接修改标头(它可能需要您自己实现 URLConnection)。
this is my own implementation of a simple Http client including cookies management. Maybe it will be useful for you. But I'm not sure if header modification is directly possible (it may require your own implementation of URLConnection).
虽然我很欣赏 Dispatch 库的所有价值,但它的语法仍然让我有点困惑。
有人引导我访问 前几天的 scalaj-http 这似乎更容易一些
While I appreciate the Dispatch library for all it's worth, the syntax still confuses me a bit.
Someone directed me to scalaj-http the other day which seems a little easier
关于简单地从 URL 获取数据。如果您不想使用外部资源,那么:
对于所有其他内容,您可以从上面的答案中选择您喜欢的任何方法。
Regarding simply GETting data from URL. If you don't want to use external sources, then:
For all other stuff, you can choose any method you like from answers above.
基于 @Antonin Brettsnajdr 的回答,使用 POST 上传文件的简单版本
Based on @Antonin Brettsnajdr's answer, a simply version of uploading a file using POST
您可以使用 spray-client。缺少文档(我花了一些时间才找到如何使用查询参数发出 GET 请求),但如果您已经在使用 Spray,那么这是一个不错的选择。我们在 AI2 上使用它,而不是 dispatch 因为操作符的象征意义较小,而且我们已经在使用 Spray/Actors。
You could use spray-client. The documentation is lacking (it took me some digging to find out how to make GET requests with query parameters) but it's a great option if you are already using spray. We're using it at AI2 over dispatch because the operators are less symbolic and we're already using spray/actors.
我使用过 Dispatch、Spray Client 和 Play WS Client Library...它们都不是简单地使用或配置的。因此,我创建了一个更简单的 HTTP 客户端库,它可以让您以简单的语句执行所有经典的 HTTP 请求。
查看示例:
... 生成 ...
该库称为 Cirrus,可通过 Maven Central 获取
该文档可在 GitHub 上获取
I've used Dispatch, Spray Client and the Play WS Client Library...None of them were simply to use or configure. So I created a simpler HTTP Client library which lets you perform all the classic HTTP requests in simple one-liners.
See an example:
... produces ...
The library is called Cirrus and is available via Maven Central
The documentation is available on GitHub