title: java.io.IOException: Server returned HTTP response code: 403 when calling azure rest api through JUnit
body: <p>This is the error which i get -</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 403 for URL: https://management.core.windows.net/&lt;subscription-id&gt;
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
  </code></pre>
  
  <p>Here is my method to call Azure service management rest api -</p>
  
  <pre><code>private static InputStream processGetRequest(URL url, String keyStore,
              String keyStorePassword) throws UnrecoverableKeyException,
              KeyManagementException, KeyStoreException,
              NoSuchAlgorithmException, IOException {
          SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore,
                  keyStorePassword);
          HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
          con.setSSLSocketFactory(sslFactory);
          con.setRequestMethod("GET");
          con.addRequestProperty("x-ms-version", "2013-10-01");
          InputStream responseStream = con.getInputStream();
          return responseStream;
      }
  </code></pre>
  
  <p>URL looks like this -</p>
  
  <pre><code>String url = String
              .format("https://management.core.windows.net/%s/services/hostedservices",
                              subscriptionId);
  </code></pre>
  
  <p>This method works fine when i run as a java application. However, when i make a call to this method in the Junits, the first Test which calls this method fails and i get the above http 403 error. </p>
  
  <p>I can't find any reason as to why it fails only for the first time. Is there any other parameter which i need to set to successfully make these rest calls to azure service management api?</p>