Frage

I am trying to get the response code from a URL:

    final SSLContext sslContext = SSLContext.getInstance( "SSL" );
    Utils.noCerts(sslContext);
    SSLSocketFactory socketFactory = sslContext.getSocketFactory();

    URL url = new URL("https://abc.def.efg:1234/hij/klm.svc/nop");
    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    urlConnection.setRequestMethod("HEAD");
    urlConnection.setSSLSocketFactory(socketFactory);

    int responseCode = urlConnection.getResponseCode();
    System.out.println(responseCode);

I have tried this with multiple URLs in my network. For some, it works fine. It tells me the response code perfectly. However with other links it gives me a 401 (even though I can access it via Chrome) and gives me the following error:

2013-12-09 09:21:40.590 java[32438:1203] Unable to load realm info from SCDynamicStore

I thought it may be an authentication issue. So after creating 'urlConnection' I added the following code as well:

    String userPassword = "user:password";  //hardcoded for now
    String encoding = new BASE64Encoder().encode((userPassword).getBytes());
    urlConnection.setRequestProperty("Authorization", "Basic" + encoding);

However that still gave me a 401 and gave me the same error message as above.

Any ideas or information on what could be causing this issue would be great. Thanks in advance.

War es hilfreich?

Lösung

Look how should look Basic Access Authentication header. You create it without the space so server can't validate it.

Without space it will look like this:

Authorization: BasicQWxhZGRpbjpvcGVuIHNlc2FtZQ==
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top