How to connect Rest webservices server with MFP server?

We develop mobile applications using ionic framework in the client side and rest webservices in server side. From client side, I 'm able to connect with mfp server successfully. Now I'm trying to connect my web services server with mfp server for sending pushnotifications . But I am getting 405 error.This is the code I have written

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

This is the response I'm getting

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)

Where am I going wrong? How can I connect my rest web service server with MFP server?

Any help will be appreciated!

Upvotes: 1

Views: 220

Answers (1)

Surajit Biswas
Surajit Biswas

Reputation: 809

In this case, first I 'ld suggest not to use connection.getOutputStream(). This will create issue.

Next, Please test the connection if it is connecting or not.

You have to add Authorization parameter in setRequestProperty

Yes, I remember, I too have faced the same issue due to some certificate error and I had to import the certificate in Java level and after that it worked. (Although I faced some other challenge (Multiple connection) issue afterwards, but that too worked...see here)

Anyways, you try the below code and if it is yet not connecting, please share the exception message

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

Upvotes: 2

Related Questions