Java 如何连接RESTWebServices服务器和MFP服务器?

Java 如何连接RESTWebServices服务器和MFP服务器?,java,rest,ionic-framework,ibm-mobilefirst,Java,Rest,Ionic Framework,Ibm Mobilefirst,我们在客户端使用离子框架开发移动应用程序,在服务器端使用rest Web服务。从客户端,我能够成功地连接到mfp服务器。 现在,我正在尝试将我的web服务服务器与mfp服务器连接,以发送推送通知。但我得到了405错误。这是我写的代码 URLConnection connection = new URL("http://localhost:9441/mfp/api/az/v1/token").openConnection(); connection.setDoOutput(true); //

我们在客户端使用离子框架开发移动应用程序,在服务器端使用rest Web服务。从客户端,我能够成功地连接到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)
我哪里做错了?如何将rest web服务服务器与MFP服务器连接


任何帮助都将不胜感激

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

接下来,测试连接是否连接

您必须在
setRequestProperty

是的,我记得,由于一些证书错误,我也遇到了同样的问题,我不得不在Java级别导入证书,然后它就工作了。(虽然后来我遇到了其他一些挑战(多重连接)问题,但这也起到了作用……请参阅)

无论如何,您尝试下面的代码,如果尚未连接,请共享异常消息

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

非常感谢你,苏拉吉特。。现在我可以连接服务器了。:)你能点击我答案旁边的“箭头”接受答案吗?你用这个连接向手机发送推送通知吗?事实上,我从来没有尝试过…但我认为,这是可能的。我可以使用你提供的URL连接MFP服务器。但我的要求是使用此URL从服务器获取访问令牌:使用此URL时,我得到以下错误java.io.IOException:服务器返回HTTP响应代码:405 for URL: