【问题标题】:URLConnection is returning text/html content typeURLConnection 正在返回 text/html 内容类型
【发布时间】:2014-04-18 21:12:24
【问题描述】:

我正在尝试获取在线 pdf 文件的输入流,但程序不工作。 URLConnection 将 url 的 content type 返回为 text/html 而不是 application/pdf。如您所见,https://www.dropbox.com/s/ao3up7xudju4qm0/Amalgabond%20Adhesive%20Agent.pdf url 是 pdf。

我正在为URLConnection 使用以下代码并获取Content Type

URL fileUrl;
try {               
String str = "https://www.dropbox.com/s/ao3up7xudju4qm0/Amalgabond%20Adhesive%20Agent.pdf"
fileUrl = new URL(str);
URLConnection connection = fileUrl.openConnection();
Log.i("mustang", "Content-type: " + connection.getContentType());
InputStream is = fileUrl.openStream();
Log.i("mustang", "is.available(): " + is.available());

因此,我无法解析缓冲区。为什么我收到 text/html 内容类型?

谢谢,

【问题讨论】:

    标签: httpurlconnection urlconnection


    【解决方案1】:

    Dropbox 使用用户代理嗅探来确定它是否应该显示灯箱(PDF 预览)。您看到的是灯箱代码(如果您打印了内容,您就可以知道这一点)。

    您需要添加一行来指定非交互式用户代理,例如 wget,方法是添加一行:

    URLConnection connection = fileUrl.openConnection();
    connection.setRequestProperty("User-Agent", "Wget/5.0");
    

    这通常会覆盖 Dropbox 的智能内容预览代码。

    【讨论】:

    • 感谢您的帮助。现在我收到内容类型application/pdfinputstream is.available() 显示0 这意味着URLConnection 正在返回empty 对吗?
    • 不一定,我在android上看到过这种行为,但这并不意味着没有数据——你应该尝试阅读,看看是否正在返回数据。
    猜你喜欢
    • 2014-09-17
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多