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

标签 java web-services http liferay

我有用 Liferay 编写的网络服务。我想获取在单独计算机上运行的 Java 客户端中的数据。我该如何执行此操作? 我已经尝试过,但出现以下错误

{"message":"需要经过身份验证的访问","exception":"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 服务需要基本身份验证。 您需要在 base64 中设置 test@liferay.com 和任意密码并将其传递。

使用 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();

     }

请注意

String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
        post.setHeader("Authorization", "Basic " + encoding);

关于java - 如何从java客户端使用liferay编写的liferay web服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43280942/

相关文章:

java - 如何通过 Socket 找到 HTTP 请求的响应时间

ruby - 通过 302 重定向维护 session 和 cookie

java - 如何在大型项目中安排/管理不同功能的方法

java - Hibernate 更改我的字段名称

java - 在 bootstrap.yml 中设置 Activity 配置文件不会影响 application.yml 的评估。这是一个错误,还是我不认识的功能?

java - Lucene-6.4.2 : No class found DefaultSimilarity, 尝试添加查询。

web-services - 这是WSS3.0 Web服务查询中的错误吗?

java - 使用 oAuth 2.0 保护基于 Jersey 的 REST 服务

php - Apache 服务器需要更长的时间

作为 Windows 服务的 Python XML-RPC 服务器