2013-10-31 32 views
0

我可以连接到一个HTTP的流媒体直播手动使用套接字这样的:Java的TCP/IP:HTTP的连接中直播

Socket radio = new Socket(); 
radio.connect(new InetSocketAddress("streaming.fueralle.org", 8000)); 
PrintWriter out = new PrintWriter(radio.getOutputStream()); 
out.println("GET /corax.mp3 HTTP/1.0"); 
out.println("User-Agent: Wget/1.8.2"); 
out.println("Host: streaming.fueralle.org:8000"); 
out.println("Accept: */*"); 
out.println("Connection: Keep-Alive"); 
out.println(""); 
out.flush(); 

DataInputStream is = new DataInputStream(radio.getInputStream()); 
String headerLine = is.readLine(); 
while(headerLine.length() > 0){ 
    resp.getWriter().println("next line is " + headerLine); 
    headerLine = is.readLine(); 
} 

现在我必须做URL和HttpURLConnection的这种连接,而不是(我计划从Google App Engine运行它,它不允许使用Socket)。但我似乎错过了一个重要的位。如果我尝试像heise.de这样的静态页面,它可以工作。但我无法开始阅读连续的流。

编辑2: 我已经把源(和整个项目)放在github中,我想念一件大事吗? https://github.com/flaschenpost/GoogleAppRadio/blob/master/MGRadio/src/de/gergele/mgradio/MGRadioServlet.java

这是一段代码。

URL u = new URL("http://streaming.fueralle.org:8000/corax.mp3"); 
// URL u = new URL("http://www.heise.de"); 
System.out.println("trying to connect!" + u); 
HttpURLConnection conn = (HttpURLConnection)u.openConnection(); 
conn.addRequestProperty("User-Agent", "MGet/1.0.0"); 
conn.addRequestProperty("Accept", "*/*"); 
// conn.addRequestProperty("Connection", "Keep-Alive"); 
// EDIT: I tried with and without setChunkedStreamingMode, I hoped it would 
//  tell the connection-object about the streaming mode from the server 
conn.setChunkedStreamingMode(4096); 
System.out.println("setup finished..." + conn + " " + conn.getRequestProperties().toString()); 
System.out.println(" type: " + conn.getContentType()); 

DataInputStream is = new DataInputStream(conn.getInputStream()); 

但是,在从头文件中读取单个字符之前,会导致TimeoutException。

所以,现在我的问题:我可以调整HTTP连接与套接字一样成功吗?我真的必须写我自己的URLStreamHandlerFactory吗?听起来有点奇怪... ...

wget和curl很容易地得到这个流,我已经花了半夜的时间来发现Java URL似乎包含了很多魔术,对于那些直播流。

感谢您的帮助!

+1

是'conn.setChunketStreamMode(4096);'真的需要吗?你看,'GET'请求不能有一个身体 - 但如果他们这样做,服务器会等待它到达而丢弃它。 –

+0

我尝试过使用和不使用ChunkedStremMode,我认为它可以给javas URL处理提示不要等待来自服务器的内容大小。虽然没有或没有它,它不起作用。 :-( – flaschenpost

+0

嗯,我注释了'conn.setChunkedStreamingMode(4096);',第二个代码,附加了'String headerLine = is.readLine(); while(headerLine.length()> 0){system.out .println(“next line is”+ headerLine); headerLine = is.readLine();}',工作得很好:它向控制台写入了大量的噪音 –

回答

0

您不能使用谷歌应用程序引擎进行直播。 GAE框架对servlet执行时间proof有时间限制。如果你去做后台任务(后端gae),你将不得不支付。而且您仍然只能保存流,而不能流式传输。

+0

因此,我不应该让它在Servlet中运行,谢谢你的评论,所以它真的用于编程我的ARM网络框。 – flaschenpost