Athista lakshmi selvaraj Athista lakshmi selvaraj - 1 year ago 262
reST (reStructuredText) Question

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", "");
InputStream response = connection.getInputStream();

This is the response I'm getting

Exception in thread "main" Server returned HTTP response code: 405 for URL:
at com.sai.laps.webservice.server.authentication.mfp.main(

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

Any help will be appreciated!

Answer Source

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

                    String authString = wsUserName+":"+wsPassword;
                    byte[] byteAuthStr = authString.getBytes();
                    String authBase64Str = Base64.encode(byteAuthStr);
                URL url = new URL(wsURL);
                URLConnection  conn =  url.openConnection();
                HttpURLConnection connection = (HttpURLConnection)conn;

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestProperty("Authorization", "Basic "+authBase64Str);
               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
               System.out.println(url + " **unknown response code**.");
               break ; // abort
    }catch(Exception ex){
                System.err.println("Error creating HTTP connection");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download