2012-07-30 4 views
1

자바를 사용하여이 사이트에서 HTML 페이지 소스 콘텐츠를 얻으려고합니다 : "http://207.200.96.231:8008". 그러나 Java의 기본 라이브러리는이 점에서 도움이되지 않았습니다. 또한이 tutorial을 사용해 보았지만 작동하지 않았습니다. 문제는 사이트의 보안 보호 때문에 발생한다고 생각합니다. 아래에 제공된 코드를 실행하면 예외가 발생합니다 : java.io.IOException: Invalid Http response.웹 페이지의 소스 코드를 가져 오지 못했습니다.

코드를 구현하는 방법에 대한 아이디어가 있으십니까? 아니면 내 필요에 부응 할 수있는 도서관이 있습니까? 지금까지 나는 JSoupJericho HTML Parser가 내가 제공 한 사이트에 연결하는 다른 접근법을 사용할 것이라고 생각했지만 시도도 실패했습니다.

String urlstr = "http://72.26.204.28:9484/played.html"; 

try { 

    URL url = new URL(urlstr); 

    URLConnection urlc = url.openConnection(); 

    InputStream stream = urlc.getInputStream(); 
    BufferedInputStream buf = new BufferedInputStream(stream); 

    StringBuilder sb = new StringBuilder(); 

    while (true){ 

    int data = buf.read(); 

    if (data == -1) 
     break; 
    else 
     sb.append((char)data); 
    } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
} 

EDIT (해결 문제) :Karai17trashgod의 도움으로 나는이 문제를 해결하기 위해 관리. Shoutcast 페이지에는 내용에 액세스하기위한 사용자 에이전트가 필요합니다. 최신 코드는 다음과 같습니다

urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0"); 

:

try { 
     URL url = new URL("http://207.200.96.231:8008/7.html"); 
     HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
     urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0"); 

     InputStream is = urlConnection.getInputStream(); 
     BufferedInputStream in = new BufferedInputStream(is); 
     int c; 
     while ((c = in.read()) != -1) { 
      System.out.write(c); 
     } 
     urlConnection.disconnect(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
} 

답변

1

이 스트림 Winamp을 필요로 나타납니다 그래서 우리가해야 할 모든이 코드를 추가하는 것이 었습니다.

 
$ curl -v http://207.200.96.231:8008 
* About to connect() to 207.200.96.231 port 8008 (#0) 
* Trying 207.200.96.231... connected 
* Connected to 207.200.96.231 (207.200.96.231) port 8008 (#0) 
It appears to require [Winamp][2]. 

> GET/HTTP/1.1 
> User-Agent: curl/... 
> Host: 207.200.96.231:8008 
> Accept: */* 
> 
ICY 200 OK 
icy-notice1:
This stream requires Winamp
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.93atdn
icy-name:Absolutely Smooth Jazz - SKY.FM - the world's smoothest jazz 24 hours a day icy-genre:Soft Smooth Jazz icy-url:http://www.sky.fm/smoothjazz/ content-type:audio/mpeg icy-pub:1 icy-br:96 ...

부록 :이 같은 스트림을 읽을 수는 :

URL url = new URL("http://207.200.96.231:8008"); 
URLConnection con = url.openConnection(); 
InputStream is = con.getInputStream(); 
BufferedInputStream in = new BufferedInputStream(is); 
int c; 
while ((c = in.read()) != -1) { 
    System.out.write(c); 
} 
+0

죄송합니다 나는 그것을하지 않았다. 소스 코드를 얻기 위해 java와 함께 winamp를 사용하는 방법이 있습니까? –

+0

윈앰프에 대해 모르겠지만 위와 같이 스트림을 읽을 수 있습니다. – trashgod

+0

큰 차이는 없다고 생각합니다. 제공 한 코드가 내 문제를 해결하지 못했습니다. 미안합니다. –

관련 문제