2012-07-01 22 views
5

Tôi đang cố gắng xác thực url https, nhưng iam bị ngoại lệ. Dưới đây là mã.nhận lỗi java.io.IOException: Máy chủ trả lại mã phản hồi HTTP: 401 cho

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.io.StringWriter; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLEncoder; 

import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLSession; 

public class Authenticate { 

    /** 
    * @param args 
    */ 
    public void authenticateUrl() { 

     HostnameVerifier hv = new HostnameVerifier() { 

      @Override 
      public boolean verify(String urlHostName, SSLSession session) { 
       System.out.println("Warning: URL Host: " + urlHostName 
         + " vs. " + session.getPeerHost()); 
       return true; 
      } 
     }; 
     // Now you are telling the JRE to trust any https server. 
     // If you know the URL that you are connecting to then this should 
     // not be a problem 
     try { 
      trustAllHttpsCertificates(); 
     } catch (Exception e) { 
      System.out.println("Trustall" + e.getStackTrace()); 
     } 
     HttpsURLConnection.setDefaultHostnameVerifier(hv); 
     StringWriter sw = new StringWriter(); 
     PrintWriter pw = new PrintWriter(sw); 
     try { 
      URL url = new URL(
        "www.stackoverflow.com"); 

      // Popup Window to request username/password password 
      // MyAuthenticator ma = new MyAuthenticator(); 
      String userPassword = "user" + ":" + "pass"; 

      // Encode String 
      String encoding = URLEncoder.encode(userPassword, "UTF-8"); 

      // or 
      // String encoding = Base64Converter.encode 
      // (userPassword.getBytes()); 

      // Need to work with URLConnection to set request property 
      URLConnection uc = url.openConnection(); 

      uc.setRequestProperty("Authorization", "UTF-8" + encoding); 
      InputStream content = (InputStream) uc.getInputStream(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(
        content)); 
      String line; 
      while ((line = in.readLine()) != null) { 
       pw.println(line); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      pw.println("Invalid URL"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      pw.println("Error reading URL"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     sw.toString(); 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Authenticate au = new Authenticate(); 
     au.authenticateUrl(); 
    } 

    // Just add these two functions in your program 

    public static class TempTrustedManager implements 
      javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
     } 

     public boolean isServerTrusted(
       java.security.cert.X509Certificate[] certs) { 
      return true; 
     } 

     public boolean isClientTrusted(
       java.security.cert.X509Certificate[] certs) { 
      return true; 
     } 

     public void checkServerTrusted(
       java.security.cert.X509Certificate[] certs, String authType) 
       throws java.security.cert.CertificateException { 
      return; 
     } 

     public void checkClientTrusted(
       java.security.cert.X509Certificate[] certs, String authType) 
       throws java.security.cert.CertificateException { 
      return; 
     } 
    } 

    private static void trustAllHttpsCertificates() throws Exception { 

     // Create a trust manager that does not validate certificate chains: 

     javax.net.ssl.TrustManager[] trustAllCerts = 

     new javax.net.ssl.TrustManager[1]; 

     javax.net.ssl.TrustManager tm = new TempTrustedManager(); 

     trustAllCerts[0] = tm; 

     javax.net.ssl.SSLContext sc = 

     javax.net.ssl.SSLContext.getInstance("SSL"); 

     sc.init(null, trustAllCerts, null); 

     javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(

     sc.getSocketFactory()); 

    } 
} 

Ngoại lệ:

java.io.IOException: Server returned HTTP response code: 401 for URL: 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) 
    at Authenticate.authenticateUrl(Authenticate.java:62) 
    at Authenticate.main(Authenticate.java:84) 

Hãy thể bất cứ ai đề nghị làm thế nào để giải quyết vấn đề này.

+0

Tôi hy vọng đó không phải là userID và password thực sự của bạn ... nếu nó là, *** THAY ĐỔI CNTT NGAY *** –

Trả lời

13

Mã lỗi 401 có nghĩa là "Không được phép". Tôi tin rằng mã của bạn không mã hóa chính xác tiêu đề Xác thực. Giả sử máy chủ hy vọng một Truy cập cơ bản Xác thực mã sẽ giống như thế này:

String credentials = "ptt" + ":" + "ptt123"; 
String encoding = Base64Converter.encode(credentials.getBytes("UTF-8")); 
URLConnection uc = url.openConnection(); 
uc.setRequestProperty("Authorization", String.format("Basic %s", encoding)); 

Một mô tả toàn diện về HTTP cơ bản và tiêu hóa lược đồ xác thực có sẵn trong RFC 2617

+1

Tuyệt vời Fantastic Cảm ơn rất nhiều, nó làm việc cho tôi. – developer

+1

+1 nó làm việc cho tôi ... nhưng tôi thay vì sử dụng 'String encoding = org.apache.catalina.util.Base64.encode (credentials.getBytes (" UTF-8 "));' Cảm ơn một triệu –

+0

+1 tôi đã đấu tranh với bộ mã hóa đúng. điều này đã giúp tôi. Tôi đã sử dụng 'org.apache.commons.codec.binary.Base64' là * sai * để cấp phép Cơ bản. ** Đúng ** là một trong những bạn bởi org.apache.tools.ant.util.Base64Converter' – mattymanme

0

Ở đây bạn có thể xử lý Error code 401 . Sử dụng HTTPURLCONNECTION đây là mã của tôi xin vui lòng kiểm tra xem bạn có thể giúp đỡ này

URL Url = new URL(<your url string>); 
HttpURLConnection connection = (HttpURLConnection) Url.openConnection(); 
connection.setRequestProperty(<your request header); 
connection.setRequestMethod("GET"); 
connection.setDoInput(true); 
connection.connect(); 

int responseCode = connection.getResponseCode(); 

if (responseCode == 200) 
    { InputStream is = connection.getInputStream(); 
     if (is != null) 
     { BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
         response = rd.readLine(); 
        } 
} else { InputStream is = connection.getErrorStream(); 

      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

     response = rd.readLine(); 

} if (response != null) 
     AppLog.e("Response-->", response); 
2

Một cách đơn giản khác là sử dụng Authenticator.

Từ các tài liệu

Lớp Authenticator đại diện cho một đối tượng mà biết làm thế nào để có được chứng thực cho một kết nối mạng. Thông thường, nó sẽ làm điều này bằng cách nhắc người dùng để biết thông tin.

URL url = null; 
try { 
    url = new URL("YOUR_URL"); 
    Authenticator.setDefault(new Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("YOUR_USERNAME","YOUR_PASSWORD".toCharArray()); 
     } 
    }); 
}catch (MalformedURLException ex) { 
     e = new WebServiceException(ex); 
}