【问题标题】:Java TCP/IP: HTTP-Connection to LivestreamJava TCP/IP:与直播的 HTTP 连接
【发布时间】:2013-10-31 14:25:10
【问题描述】:

我可以使用这样的套接字手动连接到 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();
}

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

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

这是一个sn-p。

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-Connection 调整为与 Socket-Connection 一样成功?我真的必须编写自己的 URLStreamHandlerFactory 吗?听起来有点奇怪……

wget 和 curl 很容易得到那个流,我已经花了半个晚上才发现 Java URL 似乎对那些直播有很多魔力。

感谢您的帮助!

【问题讨论】:

  • 真的需要conn.setChunketStreamMode(4096); 吗?你看,GET 请求不能有主体——但如果有,服务器会等待它到达并丢弃它。
  • 我尝试了使用和不使用 ChunkedStremMode,我认为它可以提示 javas URL-Handling 不要等待来自服务器的 Content-Size。尽管无论有没有它都不起作用。 :-(
  • 好吧,我注释掉了conn.setChunkedStreamingMode(4096);,第二个代码加上String headerLine = is.readLine(); while(headerLine.length() > 0){ system.out.println("next line is " + headerLine); headerLine = is.readLine(); },工作得很好:它向控制台写入了大量的噪音。
  • 奇怪...我会尝试 POJO 版本,但我无法想象上下文变化如此之大。我已经在我原来的问题中添加了完整的源代码。

标签: java http httpurlconnection live-streaming


【解决方案1】:

您不能使用谷歌应用引擎进行直播。 GAE框架对servlet执行时间proof有时间限制。如果您执行后台任务(gae 中的后端),您将需要付费。而且您仍然只能保存流,而不能实时流式传输。

【讨论】:

  • 所以我不应该尝试让它在 Servlet 中运行。谢谢你的评论。所以它真的涉及到在我的 Network box 中对 ARM 进行编程。
猜你喜欢
  • 2012-01-12
  • 2022-08-08
  • 2012-04-12
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
相关资源
最近更新 更多