Java Reference
In-Depth Information
import org.apache.commons.httpclient.methods.PostMethod;
/**
* Invokes the User service to add a resource and then
* retrieve it to prove it worked.
*/
public class UserClient {
private static String SERVICE_URL =
"http://localhost:8080/restexamples/resources/user/";
public static void main(String... args) {
System.out.println("Creating new user.");
createUser(777, "eben");
getUser(777);
System.out.println("All done.");
}
private static void createUser(int id, String username) {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(SERVICE_URL + id);
String userXml = "<?xml version='1.0' encoding='UTF-8' ?>" +
"<user>" +
"<id>" + id + "</id>" +
"<username>" + username + "</username>" +
"</user>";
post.setRequestBody(userXml);
try {
int httpStatus = client.executeMethod(post);
if (HttpStatus.SC_OK == httpStatus) {
String xmlResponse = post.getResponseBodyAsString();
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//clean up
post.releaseConnection();
}
}
Search WWH ::




Custom Search