How to consume liferay web service written in liferay from java client? -


i have web service written in liferay. want data in java client running on separate machine.how can this? have tried getting following error

{"message":"authenticated access required","exception":"java.lang.securityexception"}

my code below

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url;  public class javaclient {      // http://localhost:8080/restfulexample/json/product/get     public static void main(string[] args)     {          try         {              // url url = new             // url("http://localhost:8080/api/jsonws/us-pharmacy-ui-portlet.ph_fax/get-documents-to-fax             // \-u test@liferay.com:test");             httpurlconnection conn = (httpurlconnection) geturl().openconnection();              conn.setrequestmethod("get");             conn.setrequestproperty("accept", "application/json");              if(conn.getresponsecode() != 200){ throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); }              bufferedreader br = new bufferedreader(new inputstreamreader((conn.getinputstream())));              string output;             system.out.println("output server .... \n");             while ((output = br.readline()) != null)             {                 system.out.println(output);             }              conn.disconnect();          }         catch (malformedurlexception e)         {              e.printstacktrace();          }         catch (ioexception e)         {              e.printstacktrace();          }      }      private static url geturl() throws malformedurlexception     {         string url = "http://localhost:8080";         string screenname = "shahid.rana";         string password = "123";          int pos = url.indexof("://");         string protocol = url.substring(0, pos + 3);         string host = url.substring(pos + 3, url.length());          stringbuilder sb = new stringbuilder();         sb.append(protocol);         sb.append(screenname);         sb.append(":");         sb.append(password);         sb.append("@");         sb.append(host);         sb.append("/api/jsonws/us-pharmacy-ui-portlet.ph_fax/get-documents-to-fax/");         // sb.append(servicename);         system.out.println("sb.tostring()" + sb.tostring());         return new url(sb.tostring());     }  } 

yes, error because liferay web services needs basic auth. need set test@liferay.com & whatever-password in base64 & pass along.

example code using httpclient

httphost targethost = new httphost("localhost", 8080, "http");            defaulthttpclient httpclient = new defaulthttpclient();            basichttpcontext ctx = new basichttpcontext();            // plugin context use liferay 6.1            httppost post = new httppost("/api/jsonws/country/get-countries");            base64 b = new base64();         string encoding = b.encodeasstring(new string("test@liferay.com:test").getbytes());         post.setheader("authorization", "basic " + encoding);            list<namevaluepair> params = new arraylist<namevaluepair>();            //params.add(new basicnamevaluepair("emplyeeid", "30722"));            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();       } 

please note

string encoding = b.encodeasstring(new string("test@liferay.com:test").getbytes());         post.setheader("authorization", "basic " + encoding); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -