none
Not able to access any of the Azure REST API services from my machine

    Question

  • I was trying to access Azure Rest API services from my machine but I am always getting response code as 400.

    Everything looks to be fine but not sure why am not able to connect. Please share if anyone had similar kind of issue and how to resolve this.  I tried below in the rest client and got 403 forbidden error.

    management.core.windows.net/<subscription-id>/locations using https

    I have also supplied the headers Content-Type and x-ms-version.

    Please let me know if I am missing anything and also share if anyone faced simiar kind of issue and how to resolve it ?

    Tuesday, October 09, 2012 8:05 AM

Answers

  • Hi All ,

      Thanks for the responses. Really no clue for the issue and finally it got worked for me.  I was using jdk1.0.6_20 after updating it to jdk1.7 the issue got resolved. 

    How jdk versions is causing an issue here ? Any idea ?

     

    Wednesday, October 10, 2012 1:15 PM

All replies

  • Will it be possible for you to post your code here? Seems you're missing one minor thing. Looking at your code would help finding this issue.

    Thanks

    Tuesday, October 09, 2012 9:33 AM
  • public static void main(String args[]) throws Exception
        {
         System.setProperty("https.Host", "<host>");
         System.setProperty("https.Port", "<port>");     
         String reqUrl="management.core.windows.net/<subscription-id>/locations ";
         URL url = new URL(reqUrl);   
         HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
         File f = new File("D:\\Az\\testcert2.p12");
         InputStream is = new FileInputStream(f);
         byte[] fileBytes = new byte[is.available()];
         is.read(fileBytes);
         char[] keystorepassword ="".toCharArray();
         char[] keypassword = "".toCharArray();     
         InputStream certStream = getNewCertStream(fileBytes);
         connection.setSSLSocketFactory(getSSLSocketFactory(certStream, keystorepassword, keypassword));
         connection.setRequestMethod("GET");
         connection.disconnect();
         connection.setRequestProperty("Content-Type", "application/xml");
         connection.setRequestProperty("x-ms-version", "2012-03-01");
         byte buffer[] = new byte[8192];
      int read = 0;
        // signRequestSK(connection, account, key);
      
            connection.connect();
          
      int respCode = connection.getResponseCode();
        }    This is the code for which am getting response code as always 400.  Let me know what is the minor thing which am missing here.
    Tuesday, October 09, 2012 1:32 PM
  • Hi,

    This looks like Java code. I'm not familiar with Java, but do you actually get 400 or 403? You have mentioned both in your original post. If it's a 403 error, please check if the code to setup the certificate is correct. Make sure you've uploaded the certificate to the portal.

    In addition, I'm not sure whether p12 certificates are supported. The portal allows you to upload cer files. So please try to use a cer certificate. If it's 400, then please try to parse the resposne returned by the service and see if it provides more details.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Tuesday, October 09, 2012 2:40 PM
    Moderator
  • Not really a Java expert but the code looks fine. I tried the same in .Net and was able to get the data back.

    I have 2 questions for you:

    1. When you see the respCode, is it 400 or 403?

    2. Have you replaced the "<subscription-id>" in your URL with your actual subscription id? I know it's kind of dumb suggestion but I leave it there in my link, I get a 403 error.

    Tuesday, October 09, 2012 2:46 PM
  • As a side note, it makes no sense to set a Content-Type header on a GET request. I doubt this will actually cause you problems, but I thought I'd mention it just in case.
    Tuesday, October 09, 2012 4:03 PM
  • The following code worked for me. I got my certificate by downloading a .publishsettings file from the portal, base64 decoding the pkcs12 cert in there, and then adding a password with "openssl pkcs12 -nodes -in cert.p12 | openssl pkcs12 -export -out cert2.p12". (Java doesn't seem to be able to handle an empty password.)

    import java.net.*;
    import javax.net.ssl.*;
    import java.io.*;
    import java.security.*;
    
    public class Mgmt
    {
        private static SSLSocketFactory getSSLSocketFactory(File pKeyFile, String pKeyPassword) throws Exception
        {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
    
            InputStream keyInput = new FileInputStream(pKeyFile);
            keyStore.load(keyInput, pKeyPassword.toCharArray());
            keyInput.close();
    
            keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());
    
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
    
            return context.getSocketFactory();
        }
    
        public static void main(String args[]) throws Exception
        {
            String subscriptionId = args[0];
            String certFile = args[1];
            String certPassword = args[2];
    
            URL url = new URL("https://management.core.windows.net/" + subscriptionId + "/locations");
            HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("x-ms-version", "2012-03-01");
            connection.setSSLSocketFactory(getSSLSocketFactory(new File(certFile), certPassword));
            System.out.println(connection.getResponseCode());
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            int c;
            while ((c = in.read()) != -1) {
                System.out.write(c);
            }
            System.out.println();
        }
    }
    

    Tuesday, October 09, 2012 6:17 PM
  • If the above code doesn't work for you, my assumption is that your certificate isn't properly associated with your subscription.
    Tuesday, October 09, 2012 6:18 PM
  • Hi Gaurav ,

       Thanks for the reply. The response code am getting is 400. Yes, I have replaced with my subscription id in the url. The same code is working fine in my peer s machine hence not sure of the issue here.

     

    Wednesday, October 10, 2012 5:48 AM
  • Hi Ming Xu,

      Thanks for the reply. What am getting is 400. When I print, response message it is showing as "Bad Request".  The same code is working fine in my peer s machine hence not sure of the issue here.

     

    Wednesday, October 10, 2012 5:50 AM
  • Hi Steve ,

       How to fix it ?. How to ensure that proper association is there between the certificate and the subscription. But the same code is working in my peer s machine hence not sure what is causing the issue.

    Thanks

    Umapathy.d


    • Edited by UmapathyD Wednesday, October 10, 2012 7:15 AM
    Wednesday, October 10, 2012 5:57 AM
  • I see your updates. If it's a 400 error, and this code is working on other computers (with the same subscription ID and certificate), then it's not an issue with the code or with the certificate. You should look at the differences between your computers and the other computers (where this works). Are they on the same network? Are they the same operating system? Same version of Java? Same proxy settings?
    Wednesday, October 10, 2012 10:38 AM
  • Hi All ,

      Thanks for the responses. Really no clue for the issue and finally it got worked for me.  I was using jdk1.0.6_20 after updating it to jdk1.7 the issue got resolved. 

    How jdk versions is causing an issue here ? Any idea ?

     

    Wednesday, October 10, 2012 1:15 PM
  • Hi,

    I'll close this thread as your problem has been solved. As for the JDK version problem, you can also try to post it on a Java forum.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, October 11, 2012 9:49 AM
    Moderator