java https:// requests: HTTPclient or (command-line + 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 Jav开发者_如何学JAVAa, that seems like it would be easier, if more of a pain.
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.
Try Jetty + SSL
Not exactly straight-forward but well-documented and it works.
精彩评论