ColdFusion-HTTP块缺少第一个字符 [英] ColdFusion - HTTP chunk missing first character

查看:76
本文介绍了ColdFusion-HTTP块缺少第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是我上一个问题的延续关于ColdFusion中的HTTP块传输。
现在,我已经使用 java.net.URL 来读取这些块,这就是我尝试过的:

This question is a continuation to my previous question regarding HTTP chunk transfer in ColdFusion. Now, I have used java.net.URL to read the chunks and this is what I have tried:

<cfset local.objURL = createObject("java", "java.net.URL")
                         .init(javaCast("string", "https://test.com/abc.xml"))>

<!--- Open Connection --->
<cfset local.objConnection = local.objURL.openConnection()>

<!--- Input Stream --->
<cfset local.inputStream = local.objConnection.getInputStream()>

<!--- Read Chunks --->
<cfloop condition="true">
    <!--- Get Chunk Length --->
    <cfset local.chunkLength = local.inputStream.read()>
    <cfif local.chunkLength LT 0>
        <cfbreak>
    </cfif>

    <!--- Byte Array --->
    <cfset local.chunk = getByteArray(local.chunkLength)>
    <cfset local.offset = 0>

    <!--- Read Chunk Data --->
    <cfloop condition="local.offset LT local.chunkLength">
        <cfset local.bytesRead = local.inputStream.read(local.chunk, local.offset, local.chunkLength - local.offset)>
        <cfif local.bytesRead LT 0>
            <cfbreak>
        </cfif>
        <cfset local.offset += local.bytesRead>
    </cfloop>
    <!--- Chunk --->
    <cfdump var="#charsetEncode( local.chunk, 'utf-8' )#"><br />
</cfloop>

使用上面的代码,我能够读取数据,但是我面临的问题是每个块中的第一个字符都丢失,即

Using the code above, I am able to read the data but the problem I am facing is that the first character in each chunk is missing i.e.,


第一块为:<?xml version = 1.0 encoding = utf-8吗? < root> ,但我只有
得到?xml version = 1.0 encoding = utf-8吗?> < root>

有任何建议吗?

推荐答案

我认为这部分不正确:

<!--- Get Chunk Length --->
<cfset local.chunkLength = local.inputStream.read()>
<cfif local.chunkLength LT 0>
    <cfbreak>
</cfif>

您希望块长度位于流的开头。为什么?这是您自己的协议吗?如果您谈论的是HTTP分块,则应检查http响应标头 Transfer-Encoding 甚至是否具有值 chunked 。否则,假设内容已分块是完全错误的。
另外,您只能读取一个字节。这将意味着块长度最多可以为255个字节,这不是很灵活。 HTTP块可以比这更长,并且块大小由所有数字组成,直到换行为止,例如 1234\r\n

You expect the chunk length to be at the start of the stream. Why? is this your own protocol? If you are talking about http chunking, you should check if the http response header Transfer-Encoding even has the valuechunked. Otherwise it's simply wrong to assume the content is chunked. Also, you only read one byte. That would mean that the chunk length can be a maximum of 255 bytes, which not very flexible. HTTP chunks can be longer than that and the chunk size is made up from all digits until a line break, such as 1234\r\n.

我强烈怀疑上面的 read()总是在消耗您的< 并返回60的chunkLength,它是< 的ascii值。

I strongly suspect that the read() above is always consuming your < and returns a chunkLength of 60, which is the ascii value of <.

这篇关于ColdFusion-HTTP块缺少第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆