aspose file tools*
The moose likes Java in General and the fly likes Java with curl (http://curl.haxx.se/) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java with curl (http://curl.haxx.se/)" Watch "Java with curl (http://curl.haxx.se/)" New topic
Author

Java with curl (http://curl.haxx.se/)

Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Hi,

I am not sure if this is the correct place to post this, please feel free if the moderators can find a better place for this post. I have a general question regarding using curl (http://curl.haxx.se/) with Java. I have a web-application running in tomcat, based on certain actions in my web-applications I need to write a java code which would trigger curl. Curl would then query a third-party application and return an XML/JSON.

this has to be read by me and return appropriate response to the user.

Can this be done ? I see that curl has a binary distribution for Linux, once I download it (http://curl.haxx.se/download.html) how do I call this from my java web-app.

thanks !
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Does it have to be curl? There are any number of ways to generate HTTP requests in Java.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
I asked the same question to my boss and the answer was yes. Just for curiosity what other ways are you referring to ,I know httpConnection class can be used.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

There are the classes in java.net, and there are any number of 3rd-party implementations such as HttpClient.
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
thanks but how about doing it with curl ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Seems to me that calling out to a command line tool to do this is the worst possible approach.

But it looks as if there's a Java binding -- you might want to check that out.
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Hi,

Looking at all the cases/possibilities, i have opted to do this using httpclient or urlconnection class. I have not written the complete code but have just manage to write which would get us started atleast..

If I had used curl - I would have to write the following.



this would have returned me an output in XML

with httpclient i think this could be done with something like this...


With this, I have only taken care of the parameters, how to I pass the username and password with this ? any help ?
Also by default, with CURL the username and password is encoded with base64 internally, that would mean now I have to take care of 2 things

1. Encode the Username and password with base64 format
2. Accommodate the username and password in httpclient call.


Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Ok, I was able to add authentication and encoding to the code, as of now the code looks something like this.



Using this I get the return value for rCode as "406". Why am I receiving a response which is "Not Acceptable" Anything which would help me debug better and fix this.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

First of all, the setCredentials method you're calling is deprecated. You should switch to the one that takes AuthScope and Credentials objects.

Secondly, I don't think you need to base64 encode the user name and password. I've checked the man page and found absolutely nothing about base64.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anil Karamchandan
Ranch Hand

Joined: Sep 12, 2009
Posts: 47
Hi Rob,

thanks for the reply, by authsope you meant, get the "realm" String using the authScope class ? I did saw that earlier but was not able to get more info about that ? can you please elaborate ?

thanks !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

will create an AuthScope instance for host www.testabc.com on port 80 for any realm.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java with curl (http://curl.haxx.se/)