Pages

Wednesday, 23 January 2013

Consuming Liferay JSON Web Services


public class JsonTest {

public static void main(String[] args) throws Exception {

HttpHost targetHost = new HttpHost("localhost", 8080, "http");

DefaultHttpClient httpclient = new DefaultHttpClient();

httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));

AuthCache authCache = new BasicAuthCache();

BasicScheme basicAuth = new BasicScheme();

authCache.put(targetHost, basicAuth);

BasicHttpContext ctx = new BasicHttpContext();

ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

HttpPost post = new HttpPost("/<<portlet-context>>/api/secure/jsonws/sample/say-hello");

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("name", “Harish”));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");

post.setEntity(entity);

HttpResponse resp = httpclient.execute(targetHost, post, ctx);

resp.getEntity().writeTo(System.out);

httpclient.getConnectionManager().shutdown();

}
}

No comments:

Post a Comment