如何将Rest Webservices服务器与MFP服务器连接?

时间:2016-08-05 05:31:12

标签: java rest ionic-framework ibm-mobilefirst

我们使用客户端的离子框架和服务器端的 rest webservices 开发移动应用程序。从客户端,我能够成功连接mfp服务器。 现在我尝试将我的Web服务服务器与mfp服务器连接以发送推送。但我得到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)

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

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

接下来 测试连接是否正在连接。

您必须在Authorization

中添加setRequestProperty参数

是的,我记得,由于某些证书错误,我也遇到了同样的问题,我不得不在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());
            }
        }