【问题标题】:How to connect Rest webservices server with MFP server?如何将 Rest webservices 服务器与 MFP 服务器连接起来?
【发布时间】:2016-08-05 05:31:11
【问题描述】:

我们在客户端使用 ionic 框架 并在服务器端使用 rest webservices 开发移动应用程序。从客户端,我能够成功连接到 mfp 服务器。 现在我正在尝试将我的 Web 服务服务器与 mfp 服务器连接以发送 pushnotifications 。但我收到 405 错误。这是我编写的代码

URLConnection connection = new    URL("http://localhost:9441/mfp/api/az/v1/token").openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("grant_type", "client_credentials");
connection.setRequestProperty("scope", "messages.write");
connection.setRequestProperty("scope", "push.application.com.ionicframework.example854621");    
InputStream response = connection.getInputStream();
System.out.println("response"+response);

这是我收到的回复

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL: http://180.151.63.116:9441/mfp/api/az/v1/token
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at com.sai.laps.webservice.server.authentication.mfp.main(mfp.java:24)

我哪里错了?如何将我的 REST Web 服务服务器与 MFP 服务器连接?

任何帮助将不胜感激!

【问题讨论】:

    标签: java rest ionic-framework ibm-mobilefirst


    【解决方案1】:

    在这种情况下,首先我会建议不要使用 connection.getOutputStream()。这会产生问题。

    接下来,测试连接是否连接成功。

    你必须在setRequestProperty中添加Authorization参数

    是的,我记得,由于一些证书错误,我也遇到过同样的问题,我必须在 Java 级别导入证书,然后它才能工作。 (虽然我后来遇到了一些其他挑战(多连接)问题,但这也很有效......见here

    不管怎样,你试试下面的代码,如果还没有连接,请分享异常消息

    String wsURL = "https://hostservername:postnumber";
                    String wsUserName = "someUserName";
                    String wsPassword = "somePassword";
    
                    try{
                        String authString = wsUserName+":"+wsPassword;
                        byte[] byteAuthStr = authString.getBytes();
                        String authBase64Str = Base64.encode(byteAuthStr);
                        System.out.println(authBase64Str);
                    URL url = new URL(wsURL);
                    URLConnection  conn =  url.openConnection();
                    HttpURLConnection connection = (HttpURLConnection)conn;
                    connection.setDoOutput(true); 
                    /*connection.setRequestMethod("GET");
                     connection.setRequestMethod("POST");*/   
    
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.setRequestProperty("Authorization", "Basic "+authBase64Str);
                    connection.connect();
                   System.out.println( connection.getResponseCode());
                 boolean connected = false;
               switch (connection.getResponseCode()) {
               case HttpURLConnection.HTTP_OK:
                   System.out.println(url + " **OK**");
                   connected = true;
                   break; // fine, go on
               case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
                   System.out.println(url + " **gateway timeout**");
                   break;// retry
               case HttpURLConnection.HTTP_UNAVAILABLE:
                   System.out.println(url + "**unavailable**");
                   break;// retry, server is unstable
               default:
                   System.out.println(url + " **unknown response code**.");
                   break ; // abort
          }
        }catch(Exception ex){
                    System.err.println("Error creating HTTP connection");
                    System.out.println(ex.getMessage());
                }
            }
    

    【讨论】:

    • 非常感谢 Surajit.. 现在我可以连接服务器了。 :)
    • 能否请您点击我的答案旁边的“箭头”以接受答案?
    • 您是否使用此连接向移动设备发送推送通知?
    • 其实我没试过那个东西....不过我觉得是可以的。
    • 我可以使用您提供的 URL 连接到 MFP 服务器。但我的要求是使用此 URL 从服务器获取访问令牌:localhost:9080/mfp/api/az/v1/token 在使用此 URL 时,我收到以下错误 java.io.IOException: Server returned HTTP response code: 405 for URL: localhost:9080/mfp/api/az/v1/token
    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 2016-05-18
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多