2010-05-07 3 views
0

나는 plesea가 여기URL에서 pagecontent를 가져 오는 경우 연결이 항상 nopermistion을 반환합니까?

이 문자열 pagecontent

public static String getPageContent(String targetURL) throws Exception { 
     Hashtable contentHash = new Hashtable(); 
     URL url; 
     URLConnection conn; 

     // The data streams used to read from and write to the URL connection. 
     DataOutputStream out; 
     DataInputStream in; 

     // String returned as the result . 
     String returnString = ""; 

     // Create the URL object and make a connection to it. 
     url = new URL(targetURL); 
     conn = url.openConnection(); 
     // check out permission of acess URL 
     if (conn.getPermission() != null) { 
      returnString = "Do not Permission access URL "; 
     } else { 

      // Set connection parameters. We need to perform input and output, 
      // so set both as true. 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      // Disable use of caches. 
      conn.setUseCaches(false); 

      // Set the content type we are POSTing. We impersonate it as 
      // encoded form data 
      conn.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded"); 

      // get the output stream . 
      out = new DataOutputStream(conn.getOutputStream()); 
      String content = ""; 

      // Create a single String value pairs for all the keys 
      // in the Hashtable passed to us. 
      Enumeration e = contentHash.keys(); 
      boolean first = true; 
      while (e.hasMoreElements()) { 
       // For each key and value pair in the hashtable 
       Object key = e.nextElement(); 
       Object value = contentHash.get(key); 

       // If this is not the first key-value pair in the hashtable, 
       // concantenate an "&" sign to the constructed String 
       if (!first) 
        content += "&"; 

       // append to a single string. Encode the value portion 
       content += (String) key + "=" 
         + URLEncoder.encode((String) value); 

       first = false; 
      } 

      // Write out the bytes of the content string to the stream. 
      out.writeBytes(content); 
      out.flush(); 
      out.close(); 

      // check if can't read from URL 

      // Read input from the input stream. 
      in = new DataInputStream(conn.getInputStream()); 

      String str; 
      while (null != ((str = in.readLine()))) { 
       returnString += str + "\n"; 
      } 

      in.close(); 
     } 

     // return the string that was read. 
     return returnString; 
    } 

답변

0

가 있습니까 반환 코드 확인, 링크

의 pagecontent을 반환 할 methor을 가지고 있지만 그것을 실행할 때, 항상 곁에 "perrmisson하지 마십시오"를 반환 getPermission()의 의미를 올바르게 해석 했습니까? getPermission()의 javadoc에서는 [1] 상태 :

결과 :

이 URLConnection가 나타내는 접속을 위해서 필요한 액세스권을 나타내는 Permission 객체입니다.

수단하십시오 Permission 객체 반품 여부를 당신의 JVM이 또는 지정한 URL에 연결하는 JVM의 보안 정책에 부여 된 행동이 필요하지 않을 수도 있습니다 여부를 알려줍니다. 당신은 실제로 위의 코드 예제에서 허가을 확인하지 마십시오.

대부분의 런타임 클래스는 내부적으로이 작업을 수행하므로 일반적으로이 작업을 수행 할 필요가 없습니다. SecurityManager은 실제 permisson이 부여되지 않은 경우 적절한 예외를 throw합니다.

SecurityException이 던져 질 때 첫 번째 호출시 권한이 설정되어 있지 않다는 것을 알기 때문에 getPermission()을 테스트하지 않고 연결을 시작하고 작업을 시작하는 것이 좋습니다.

[1] 연결 객체와 관련된 http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#getPermission()

+0

감사합니다. 만약 내가 getPermission 그래서 내가 어떻게 403,405 반환 ..... 오류를 제어 할 수 없어 Logged – tiendv

0

권한은 연결의 다른 끝에있는 HTTP 서버의 허가와는 아무 상관이 없습니다. Ralf는 로컬 JVM 보안 정책과 관련이 있다고 언급했습니다. HttpURLConnection.getResponseCode()을 통해 HTTP 서비스 응답 상태 (예 : 200, 403 등)를 해석합니다.

관련 문제