如何从java客户端使用用liferay编写的liferay web服务?

如何从java客户端使用用liferay编写的liferay web服务?,java,web-services,http,liferay,Java,Web Services,Http,Liferay,我用Liferay编写了web服务。我想在Java客户机中获取在单独机器上运行的数据。我如何才能做到这一点? 我已经试过了,但我得到了以下错误 {消息:需要经过身份验证的访问,异常:java.lang.SecurityException} 我的代码在下面 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnec

我用Liferay编写了web服务。我想在Java客户机中获取在单独机器上运行的数据。我如何才能做到这一点? 我已经试过了,但我得到了以下错误

{消息:需要经过身份验证的访问,异常:java.lang.SecurityException}

我的代码在下面

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 from 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());
    }

}

是的,您将收到此错误,因为Liferay Web服务需要基本身份验证。 你需要设置test@liferay.com在base64中输入任何密码(&W)并将其传递

使用HttpClient的示例代码

HttpHost targetHost = new HttpHost("localhost", 8080, "http");
           DefaultHttpClient httpclient = new DefaultHttpClient();
           BasicHttpContext ctx = new BasicHttpContext();
           // Plugin Context Use for 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();

     }

Hi Parth Ghiya我收到以下错误..HttpHost无法解析为类型,BasicHttpContext无法解析为type@SajjadHussain你需要这么多jar文件。commons-codec.jar,httpclient.jar,httpcore.jar,commons-logging.jar如果你想看我的代码片段,我已经有了以下jars commons-codec-1.6.jar commons-codec-1.7.jar httpclient-4.0.jar httpclient-4.1-beta1.jar,httpcore.jar这是导入org.apache.http.HttpHost;导入org.apache.http.protocol.BasicHttpContext;它们在您的buildPath中可用吗。!你能公布准确的错误吗?你什么时候能得到它们?
String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
        post.setHeader("Authorization", "Basic " + encoding);