2011-04-01 7 views
2

J'ai besoin d'aide pour que l'authentification DIGEST fonctionne. J'utilise la bibliothèque Apache 4.1. Quand j'essaie de me connecter, je reçois.Java Digest Authentification POST XML

Exception dans le thread « principal » javax.net.ssl.SSLPeerUnverifiedException: peer non authentifié

Je suis en train de se connecter à un Asterisk Switchvox Dev Extension API, que vous envoyez un message xml et vous donner retour d'informations. J'ai certainement le nom d'utilisateur/mot de passe correct et j'ai eu ce travail sur un script PERL mais je ne peux pas l'obtenir en Java.

Voici mon code

public class Main { 

public static void main(String[] args) throws Exception { 

    HttpHost targetHost = new HttpHost("192.168.143.253", 443, "https"); 

    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    try { 
     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope("192.168.143.253", targetHost.getPort()), 
       new UsernamePasswordCredentials("username", "mypassword")); 

     // Create AuthCache instance 
     AuthCache authCache = new BasicAuthCache(); 
     // Generate DIGEST scheme object, initialize it and add it to the local auth cache 

     DigestScheme digestAuth = new DigestScheme(); 

     authCache.put(targetHost, digestAuth); 

     // Add AuthCache to the execution context 
     BasicHttpContext localcontext = new BasicHttpContext(); 
     localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); 

     HttpGet httpget = new HttpGet("https://192.168.143.253/xml/"); 

     System.out.println("executing request: " + httpget.getRequestLine()); 
     System.out.println("to target: " + targetHost); 

     for (int i = 0; i < 3; i++) { 
      HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); 
      HttpEntity entity = response.getEntity(); 

      System.out.println("----------------------------------------"); 
      System.out.println(response.getStatusLine()); 
      if (entity != null) { 
       System.out.println("Response content length: " + entity.getContentLength()); 
      } 
      EntityUtils.consume(entity); 
     } 

    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 
} 

}

Répondre

3

J'ai finalement trouvé la réponse à ma question.

public static void main(String args[]) { 

    final String username = "user"; 
    final String password = "password"; 



    Authenticator.setDefault(new Authenticator() { 
     @Override 
      protected PasswordAuthentication getPasswordAuthentication() { 
       PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray()); 
       //System.out.println(pa.getUserName() + ":" + new String(pa.getPassword())); 
       return pa; 
      } 
      }); 
    BufferedReader in = null; 
    StringBuffer sb = new StringBuffer(); 

    try { 
     //URL url = new URL(strURL); 

     HttpsURLConnection connection = (HttpsURLConnection) new URL("https://secureHost/").openConnection(); 
        connection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier()); 
        connection.setHostnameVerifier(new CustomizedHostnameVerifier()); 
        connection.setDoOutput(true); 
        connection.setDoInput(true); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Content-Type","text/xml"); 
        PrintWriter out = new PrintWriter(connection.getOutputStream()); 
        String requestString = "<request method=\"switchvox.currentCalls.getList\"></request>"; 

        out.println(requestString); 
        out.close(); 

     in = new BufferedReader(new InputStreamReader(connection 
       .getInputStream())); 

     String line; 

     while ((line = in.readLine()) != null) { 
      sb.append(line).append("\n"); 
     } 
    } catch (java.net.ProtocolException e) { 
     sb.append("User Or Password is wrong!"); 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (in != null) { 
       in.close(); 
      } 
     } catch (Exception e) { 
      System.out.println("Exception"); 
     } 
    } 

    System.out.println("The Data is: " + sb.toString()); 

} 

}

+0

Vous n'êtes pas en utilisant 'DigestScheme digestauth = new DigestScheme()' dans votre réponse. Comment cela fonctionne avec Digest sécurité (je sais que le code fonctionne, je veux savoir sur la sécurité de celui-ci) –

Questions connexes