【问题标题】:Android Http server HEAD method with Content-Length带有 Content-Length 的 Android Http 服务器 HEAD 方法
【发布时间】:2012-08-21 11:04:21
【问题描述】:

我正在尝试创建这样的响应

-------------请求---------- HEAD /external/images/media/21.jpg HTTP/1.0

getcontentFeatures.dlna.org: 1

主机:192.168.1.130:57645

------------我想要的答案------ HTTP/1.1 200 正常

日期:格林威治标准时间 2012 年 8 月 21 日星期二 10:24:59

缓存控制:无缓存

transferMode.dlna.org:流媒体

contentFeatures.dlna.org:DLNA.ORG_PN=JPEG_LRG;DLNA.ORG_OP=01;DLNA.ORG_CI=0

内容类型:图片/jpeg

最后修改时间:2012 年 2 月 25 日星期六 15:11:58 GMT

内容长度:60909

接受范围:字节

但是当我尝试输入“Content-Length:60909”时出现问题,如果我不输入“Content-Length 标头已存在”,则会引发此异常,如果我不输入标头 Content-Length 始终为 0,那么不是什么我想要。

这是我的代码:

public void handle(HttpRequest request,
                   HttpResponse response,
                   HttpContext context) throws HttpException, IOException {

String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
if(method.equals("HEAD"))
{
String objectId = getUrlBuilder().getObjectId(request.getRequestLine().getUri());
        DIDLObject obj = findObjectWithId(objectId);
        if (obj == null) {

            response.setStatusCode(HttpStatus.SC_NOT_FOUND);
            return;
        }
MimeType mimeType = getMimeType(obj);
        long sizeInBytes = getSizeInBytes(obj);



        response.setHeader("Cache-control", "no-cache");
        response.setHeader("transferMode.dlna.org", "Streaming");
        String aMimeType = mimeType.toString();
        String dlnaspec="";

        if (aMimeType.equals("image/jpeg"))
            dlnaspec = "DLNA.ORG_PN=JPEG_LRG";
        else if (aMimeType.equals("audio/mpeg"))
            dlnaspec = "DLNA.ORG_PN=MP3";
        else if (aMimeType.equals("audio/L16") || aMimeType.equals("audio/wav"))
            dlnaspec = "DLNA.ORG_PN=LPCM";

        response.setHeader("contentFeatures.dlna.org", dlnaspec+";DLNA.ORG_OP=01;DLNA.ORG_CI=0");

        response.setHeader("Content-Type", mimeType.toString());
        response.setHeader("Accept-Ranges", "bytes");
        response.setHeader("Content-Length", String.valueOf(sizeInBytes));
        response.setStatusCode(HttpStatus.SC_OK); 

 }

 }

有什么想法吗? 我也尝试过以这种方式放置实体

ByteArrayInputStream stream=new ByteArrayInputStream(new byte[(int) sizeInBytes]);
InputStreamEntity entity = new InputStreamEntity(stream, sizeInBytes); 
response.setEntity(entity);

但问题是 HEAD 方法没有主体,所以这种方法无效

【问题讨论】:

    标签: android http


    【解决方案1】:

    我找到了一种解决方案,实现我自己的 ResponseContent 并支持用户内容长度 就是代码

    public class MyCustomResponseContent implements HttpResponseInterceptor 
    {
    
    private final boolean overwrite;
    
    /**
    * Default constructor. The <code>Content-Length</code> or <code>Transfer-  Encoding</code>
    * will cause the interceptor to throw {@link ProtocolException} if already present in the
    * response message.
    */
    
    public MyCustomResponseContent() 
    {
        this(false);
    }
    
    /**
    * Constructor that can be used to fine-tune behavior of this interceptor.
    *
    * @param overwrite If set to <code>true</code> the <code>Content-Length</code> and
    * <code>Transfer-Encoding</code> headers will be created or updated if already present.
    * If set to <code>false</code> the <code>Content-Length</code> and
    * <code>Transfer-Encoding</code> headers will cause the interceptor to throw
    * {@link ProtocolException} if already present in the response message.
    *
    * @since 4.2
    */
    
    public MyCustomResponseContent(boolean overwrite) 
    {
        super();
        this.overwrite = overwrite;
    }
    
    /**
    * Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
    * @param response The HttpResponse to modify.
    * @param context Unused.
    * @throws ProtocolException If either the Content-Length or Transfer-Encoding headers are found.
    * @throws IllegalArgumentException If the response is null.
    */
    
    public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException 
    {
        if (response == null) 
        {
            throw new IllegalArgumentException("HTTP response may not be null");
        }
        if (this.overwrite) 
        {
            response.removeHeaders(HTTP.TRANSFER_ENCODING);
            response.removeHeaders(HTTP.CONTENT_LEN);
        }
        else 
        {
            if (response.containsHeader(HTTP.TRANSFER_ENCODING)) 
            {
                throw new ProtocolException("Transfer-encoding header already present");
            }
            /*
            if (response.containsHeader(HTTP.CONTENT_LEN)) 
            {
                throw new ProtocolException("Content-Length header already present");
            }
            */
        }
        ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
        HttpEntity entity = response.getEntity();
        if (entity != null) 
        {
            long len = entity.getContentLength();
            if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) 
            {
                response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
            }
            else if (len >= 0) 
            {
                response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
            }
            //  Specify a content type if known
            if (entity.getContentType() != null && !response.containsHeader(HTTP.CONTENT_TYPE )) 
            {
                response.addHeader(entity.getContentType());
            }
            //  Specify a content encoding if known
            if (entity.getContentEncoding() != null && !response.containsHeader(HTTP.CONTENT_ENCODING)) 
            {
                response.addHeader(entity.getContentEncoding());
            }
        }
        else if (!response.containsHeader(HTTP.CONTENT_LEN))            
        {
            int status = response.getStatusLine().getStatusCode();
            if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_NOT_MODIFIED && status != HttpStatus.SC_RESET_CONTENT) 
            {
                response.addHeader(HTTP.CONTENT_LEN, "0");
            }
        }       
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-20
      • 2015-03-05
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多