运行代码加载Podbean Feed XML时遇Premature end of file错误求助
Fixing "Premature end of file" When Loading Podbean Feed via Scala XML.load
Hey there! Let's break down what's going wrong here and how to fix it quickly.
What's Causing the Error?
The core issue is that Podbean's feed server blocks requests that don't include a valid User-Agent header.
- Browsers automatically send a standard User-Agent string (identifying the browser/OS), so the feed loads fine there.
curluses a default User-Agent likecurl/[version], which Podbean's server might reject, resulting in an empty response.- When you use
XML.load(new URL(feed))directly, the underlying JavaURLConnectiondoesn't send a proper User-Agent either. The server returns an empty body, and the XML parser throws the "Premature end of file" error because there's nothing to parse.
How to Fix It
You need to manually add a valid User-Agent header to your HTTP request. Here's how to adjust your Scala code to do that:
import scala.xml.XML import java.net.{URL, HttpURLConnection} val feedUrl = "https://fourfingerdiscount.podbean.com/feed/" val connection = new URL(feedUrl).openConnection().asInstanceOf[HttpURLConnection] // Add a browser-like User-Agent header to mimic a legitimate request connection.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" ) // Load the XML using the configured connection's input stream val podcastXml = XML.load(connection.getInputStream())
Verify with curl First
Before running the Scala code, you can confirm this is the problem by testing with curl and adding the User-Agent header:
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36" https://fourfingerdiscount.podbean.com/feed/
This should return the full XML feed content, confirming that the User-Agent was the missing piece.
内容的提问来源于stack exchange,提问作者Brad Cypert
相关产品推荐
相关产品推荐

