2015-09-14 22 views
9

Sto tentando di accedere a un URL che richiede l'autenticazione tramite nome utente e password.accesso a un URL protetto da password

Non ci sono errori durante la costruzione .. Mi manca qualcosa?

questa è la prima classe, imposta l'autenticatore che verrà utilizzato dal codice di rete

public class AccessPasswordProtectedURLWithAuthenticator { 

    public static void main(String[] args) { 

    try { 

     // when a proxy or an HTTP server asks for authentication. 

     Authenticator.setDefault(new Authenticator(){}); 

     URL url = new URL("http://website.html"); 

     // read text returned by server 
     BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 

     String line; 
     while ((line = in.readLine()) != null) { 
      System.out.println(line); 
     } 
     in.close(); 

    } catch (MalformedURLException e) { 
     System.out.println("Malformed URL: " + e.getMessage()); 
    } catch (IOException e) { 
     System.out.println("I/O Error: " + e.getMessage()); 

    } 

} 

} 

la seconda classe

public class CustomAuthenticator extends Authenticator{ 

    /// Called when password authorization is needed 

    protected PasswordAuthentication getPasswordAuthentication() { 

     /// Get information about the request 

     String prompt = getRequestingPrompt(); 
     String hostname = getRequestingHost(); 
     InetAddress ipaddr = getRequestingSite(); 
     int port = getRequestingPort(); 

     String username = "Administrator"; 

     String password = "Administrator"; 

     /// Return the information (a data holder that is used by Authenticator) 

     return new PasswordAuthentication(username, password.toCharArray()); 

    } 

} 
+0

state ottenendo un popup quando si tenta di accedere all'URL browser –

risposta

3

Il tuo primo segmento di codice non fa riferimento la seconda classe . Dovrebbe avere questo codice:

Authenticator.setDefault(new CustomAuthenticator()); 
+0

Buona cattura. Questa dovrebbe essere la soluzione. –

2

Hai provato ad anteporre il nome utente e la password al nome host nell'URL stesso? Questo potrebbe consentire di evitare di utilizzare l'Autenticator del tutto:

URL url = new URL("http://username:[email protected]"); 
1

Questo potrebbe essere utile.

private HttpURLConnection getJsonConnection(String urlString) throws IOException { 

    URL url = new URL(urlString); 

    HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 

    String userCredentials = "username:password"; 
    String basicAuth = "Basic " + Base64.encodeToString(userCredentials.getBytes(), Base64.DEFAULT); 
    urlConnection.setRequestProperty("Authorization", basicAuth); 


    return urlConnection; 
} 

private String getResponse(HttpURLConnection urlConnection) throws IOException { 

    BufferedReader bufferedReader = null; 

    try { 

     if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 

      bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

      while ((inputLine = bufferedReader.readLine()) != null) { 
       response.append(inputLine); 
      } 

      return response.toString(); 
     } 

    } catch (IOException e) { 

     Logger.error(e); 

    } finally { 

     if (bufferedReader != null) { 
      bufferedReader.close(); 
     } 
    } 
    return null; 
} 
0
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.Authenticator; 
import java.net.InetAddress; 
import java.net.PasswordAuthentication; 
import java.net.URL; 

public class Main { 
    public static void main(String[] argv) throws Exception { 
    Authenticator.setDefault(new MyAuthenticator()); 
    URL url = new URL("http://hostname:80/index.html"); 

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
String str; 
while ((str = in.readLine()) != null) { 
    System.out.println(str); 
} 
in.close(); 
} 
} 

class MyAuthenticator extends Authenticator { 

    protected PasswordAuthentication getPasswordAuthentication() { 
    String promptString = getRequestingPrompt(); 
    System.out.println(promptString); 
    String hostname = getRequestingHost(); 
    System.out.println(hostname); 
    InetAddress ipaddr = getRequestingSite(); 
    System.out.println(ipaddr); 
    int port = getRequestingPort(); 

    String username = "Administrator"; 
    String password = "Administrator"; 
    return new PasswordAuthentication(username, password.toCharArray()); 
    } 
} 

Controllare sopra esempio se funziona per voi

Problemi correlati