简体   繁体   English

Java TCP / IP:HTTP连接到实时流

[英]Java TCP/IP: HTTP-Connection to Livestream

I can connect to a HTTP-Livestream manually by using Sockets like that: 我可以通过使用Socket手动连接到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-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();
}

Now I have to do this connection with URL and HttpUrlConnection instead (I plan to run it from Google App Engine, which allows no Socket). 现在,我必须改为使用URL和HttpUrlConnection进行此连接(我打算从Google App Engine中运行它,不允许使用Socket)。 But I seem to be missing an important bit. 但是我似乎缺少了重要的一点。 If I try a static page like heise.de, it works. 如果我尝试使用像heise.de这样的静态页面,它将起作用。 But I can not start reading a continuous Stream. 但是我无法开始阅读连续的Stream。

EDIT2: I have put the source (and the whole project) in github, do I miss a big thing? EDIT2:我已将源代码(和整个项目)放在github中,我想念什么大事了吗? https://github.com/flaschenpost/GoogleAppRadio/blob/master/MGRadio/src/de/gergele/mgradio/MGRadioServlet.java https://github.com/flaschenpost/GoogleAppRadio/blob/master/MGRadio/src/de/gergele/mgradio/MGRadioServlet.java

Here a snippet. 这里是一个片段。

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());

But that leads to an TimeoutException before reading a single character from the headers. 但这会导致TimeoutException,然后再从标头读取单个字符。

So now my question: Howo can I tune the HTTP-Connection to be as successfull as the Socket-Connection? 所以现在我的问题是:我怎样才能将HTTP连接调整为与Socket连接一样成功? Do I really have to write my own URLStreamHandlerFactory? 我真的必须编写自己的URLStreamHandlerFactory吗? Sounds a bit weird... 听起来有点奇怪...

wget and curl get that stream easily, and I have already spent half a night to find out that Java URL seems to contain to much magic somehow for those livestreams. wget和curl可以很容易地获得该流,而我已经花了半夜的时间才发现Java URL对于这些实时流似乎包含了很多魔力。

Thanks for any help! 谢谢你的帮助!

You can't use google app engine for live streaming. 您不能使用Google App Engine进行实时流式传输。 GAE framework has a time restriction on servlet execution time proof . GAE框架在servlet执行时间证明上有时间限制。 If you go for background tasks (backend in gae) you will have to pay. 如果您要执行后台任务(gae后端),则必须付费。 And still you'll be only able to save the stream, not to stream it live. 而且,您仍然只能保存流,而不能实时流化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM