2015-09-14 171 views
9

我试图访问需要通过用户名和密码进行身份验证的网址。访问受密码保护的URL

建设时没有错误..我错过了什么吗?

这是第一类,它设置将由网络代码

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()); 

    } 

} 

} 

第二类使用的认证器

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

你得到当你试图访问浏览器 –

回答

3

第一代码段,不参考所述第二类。它应该有这样的代码:

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

良好的渔获物的URL的弹出。这应该是解决方案。 –

2

你试过前面加上用户名和密码在URL中的主机名?这可能会让您避免使用Authenticator:

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

这可能会得心应手。

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()); 
    } 
} 

检查上面的例子,如果你的作品