繁体   English   中英

打开HTTP连接后如何识别内容类型?

[英]How can I identify the content-type after opening an HTTP connection?

我正在打开与程序中生成的URL的HTTP连接:

String url = INPUT_URL;
HttpURLConnection connection;
while (true)
{
    connection = (HttpURLConnection)new URL(url).openConnection();
    connection.setInstanceFollowRedirects(true);
    switch (connection.getResponseCode()/100)
    {
    case 3:
        url = connection.getHeaderField("Location");
        break;
    case 4:
    case 5:
        // Report some error
        return;
    }
}

现在,在case 2我想确定内容类型。 例如:

我不能使用URL结构来确定这一点。 例如:

我知道如何使用InputStream对象读取内容,但是:

  1. 我希望避免在可能的情况下获取内容本身。

  2. 我看不到它如何帮助我确定内容类型。

任何想法将不胜感激...谢谢。

您可以使用getContentType

public String getContentType()在API级别1中添加

返回由响应标头字段content-type指定的内容的MIME类型;如果类型未知,则返回null。

暂无
暂无

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

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