Java: HTTP libraries

There are different ways how to make a request in Java. Considering Restlet framework:

// Prepare the data for the request
String ontology = "http://nb.vse.cz/~svabo/oaei2009/data/cmt.owl";
String instructions = "<instructions><oppl_script><remove>hasDecision domain
Paper</remove><remove>hasDecision
range Decision</remove><remove>Acceptance subClassOf
Decision</remove><add>hasDecision domain Paper</add><add>hasDecision range
Decision</add><add>Acceptance subClassOf Decision</add><add>!AcceptedPaper
equivalentTo (hasDecision some Acceptance)</add></oppl_script><entities><rename
type='ObjectProperty' original_name='hasDecision'>hasDecisionPaper</rename><entit
y type='Class' original_name='Paper'>Paper</rename><rename type='Class'
original_name='Decision'>Decision</rename></entities></instructions>";
Representation entity = new
StringRepresentation("ontology="+ontology+"&instructions+"instructions",
                MediaType.TEXT_XML);

// Prepare the request  
ClientResource resource = new
ClientResource("http://owl.vse.cz:8080/ontologyTransformation/transformation");
 

// Send the HTTP POST request
resource.post(entity);

//Do what you want with a response, e.g.:
resource.getResponseEntity().write(System.out);

Considering Java standard library, an HTTP client, the code snippet would be as follows:

// Prepare the data for the request
NameValuePair[] data = {
  new NameValuePair("ontology",
"http://nb.vse.cz/~svabo/oaei2009/data/cmt.owl"),
  new NameValuePair("binding", "<instructions><oppl_script><remove>hasDecision
domain
Paper</remove><remove>hasDecision
range Decision</remove><remove>Acceptance subClassOf
Decision</remove><add>hasDecision domain Paper</add><add>hasDecision range
Decision</add><add>Acceptance subClassOf Decision</add><add>!AcceptedPaper
equivalentTo (hasDecision some Acceptance)</add></oppl_script><entities><entity
type='ObjectProperty' original_name='hasDecision'>hasDecisionPaper</entity><entit
y type='Class' original_name='Paper'>Paper</rename><rename type='Class'
original_name='Decision'>Decision</rename></entities></instructions>")
};

// Prepare the request
HttpClient client = new HttpClient();

PostMethod post = new
PostMethod("http://owl.vse.cz:8080/ontologyTransformation/transformation");
post.setRequestBody(data);

// Execute the method.
int statusCode = client.executeMethod(post);

//Do what you want with a response, e.g.:
InputStream in = post.getResponseBodyAsStream();



ondrej 2011-06-21