java https:// 请求:HTTPclient 或(命令行 +curl)?
我需要使用 cookie 向 https:// URL 发出一些请求。我知道如何使用 cURL 从命令行执行此操作,并且希望使用 Java 实现自动化。
我正在查看 HTTPClient 并且完全感到困惑。看起来这需要大量蹩脚的编程。我有什么理由应该继续从事这一行的努力吗?
另一种方法是从 Java 内部执行 cURL 可执行文件,这看起来会更容易,但也更痛苦。
I need to make some requests with cookies to an https:// URL. I know how to do it from the command line with cURL, and would like to automate it with Java.
I'm looking at HTTPClient and am getting utterly confused. It seems like it would take a lot of grungy programming. Is there any reason I should continue to pursue this line of effort?
The other way to go is to execute the cURL executable from within Java, that seems like it would be easier, if more of a pain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅他们关于 使用 SSL 的指南,据我所知,这是与 HttpClient 4.x 相同,只是它使用 4.x 的语法略有不同(请参阅 该语法的示例)。换句话说,如果您将该示例中的“http”更改为“https”,它将完美运行。如果您正在查看本教程,我可以理解为什么您会认为它比必要的更复杂。但是,仅当您尝试访问的站点的根证书不是 JVM 接受的证书时才需要这样做。
但是,如果需要使用证书进行特殊操作,此处 是如何执行此操作的示例。坦率地说,我不知道如果将它发送到未经验证的证书,curl 会做什么,但我想至少您必须指定一些命令行选项以使其忽略它。
在 cookie 方面,curl 到底比 HttpClient 更容易实现什么?
人们希望避免从命令行调用某些内容的主要原因是它使错误处理变得困难。如果命令以错误条件结束,您实际上只能打印出它给出的错误消息 - 或者可能对错误消息进行一些处理并尝试基于此抛出异常,但是当您更改版本时,您的错误检查代码可能会失效。它还引入了您可能不希望随 jar 一起提供的外部依赖项。
另一方面,如果您使用 HttpClient,它将根据问题的具体情况抛出异常,从而允许您对错误情况做出不同的响应。
编辑:抱歉,不知何故错过了您已经在使用它的事实。以为你正在使用基于 JDK 的东西。
See their guide on Using SSL, from what I can tell it is the same HttpClient 4.x, except it uses the slightly different syntax of 4.x (see an example of that syntax). In other words, if you changed the "http" in that example to "https" it would work perfectly. If you were looking at this tutorial, I can understand why you would be thinking it is more complicated than necessary. However that is only necessary if the root certificate of the site that you are trying to access is not an accepted certificate of the JVM.
However if special things with the certificates do become necessary, here is an example of how to do that. Frankly, I don't know what curl would do if you sent it to a non-verified certificate, but I imagine that at the very least you have to specify some command-line option to get it to ignore that.
What exactly is it that curl makes easier than HttpClient when it comes to cookies?
The main reason that one would want to avoid calling something from the command-line is that it makes error handling difficult. If the command ends in an error condition, you really can only print out the error message it gives — or possibly do some processing on the error message and attempt to throw exceptions based on that, but the moment you change versions, your error-checking code would probably be invalidated. It also introduces an external dependency you probably wouldn't want to ship with your jar.
On the other hand, if you use HttpClient, it will throw exceptions based on exactly what the problem is, allowing you to respond differently to error conditions.
Edit: Sorry, somehow missed the fact that you were working with it already. Thought you were working with something JDK based.
尝试 Jetty + SSL
不完全简单,但有详细记录它有效。
Try Jetty + SSL
Not exactly straight-forward but well-documented and it works.