Java TCP/IP:到Livestream的HTTP连接
我可以使用如下套接字手动连接到HTTP Livestream:Java TCP/IP:到Livestream的HTTP连接,java,http,httpurlconnection,live-streaming,Java,Http,Httpurlconnection,Live Streaming,我可以使用如下套接字手动连接到HTTP Livestream: 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-A
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运行它,它不允许使用套接字)。但我似乎遗漏了一点重要的东西。如果我尝试像heise.de这样的静态页面,它会工作。但我无法开始连续阅读
编辑2:
我已经将源代码(以及整个项目)放在github中,我是否错过了一件大事
这里有一个片段
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很容易就得到了这个流,我已经花了半个晚上的时间来发现,对于那些livestreams来说,javaurl似乎包含了很多魔力
谢谢你的帮助 您不能使用google app engine进行流媒体直播。GAE框架对servlet执行时间有时间限制。如果你去后台工作(后台在gae),你将不得不支付。但你仍然只能保存流,而不能实时流 是
conn.setChunketStreamMode(4096)代码>真的需要吗?你看,GET
请求不能有主体,但如果有主体,服务器将等待主体到达并丢弃它。我尝试了使用ChunkedStremMode和不使用ChunkedStremMode,我认为这可以提示Java的URL处理不要等待来自服务器的内容大小。尽管不管有没有它都不起作用-(好的,我注释掉了conn.setChunkedStreamingMode(4096);
,第二个代码,附加了字符串headerLine=is.readLine();而(headerLine.length()>0){system.out.println(“下一行是”+headerLine);headerLine=is.readLine();)
,工作正常:它会给控制台带来大量噪音。奇怪的是……我会尝试POJO版本,但我无法想象上下文会发生如此大的变化。我在原始问题中添加了完整的源代码。因此我不应该让它在Servlet中运行。感谢您的评论。因此,它确实会在我的网络框中对ARM进行编程。