3

I am trying to get a file from a URL using cfhttp but it seems that the provider is sending the data in chunks.

<cfhttp
    method="get"
    url="https://www.test.com/somefile.xml">
</cfhttp>

The response header is having Transfer-Encoding as chunked and is missing Content-Length. Also, the statusCode is 200 Ok but the FileContent is showing "Connection Failure".

Any suggestions?

Abhishekh Gupta
  • 6,206
  • 4
  • 18
  • 46
  • What happens if you browse the url? – Dan Bracuk Feb 16 '16 at 13:44
  • @DanBracuk I am able to read the content of the file. – Abhishekh Gupta Feb 16 '16 at 13:51
  • I vaguely remember running across this same problem a few years ago. Try adding the following request headers: ` ` – Sam M Feb 16 '16 at 21:10
  • @SamM Tried. Same results. :( – Abhishekh Gupta Feb 17 '16 at 06:03
  • @Beginner, I wish I could be more help than that. I remember problems with chunked responses. But the website's developer was kind enough to change the responses to include a Content-Length header with the correct value (not 0), which solved the problem. Might be worth a shot to contact the site's admin. As long as Content-Length is the correct value, having it chunked or not ought to be irrelevant. – Sam M Feb 18 '16 at 04:30
  • @SamM This problem is solved using `java.net.URL` and a continuation to this question is [here](http://stackoverflow.com/questions/35451665/coldfusion-http-chunk-missing-first-character). Thanks for your response :) – Abhishekh Gupta Feb 18 '16 at 06:11
  • @Beginner I looked at your other question and am impressed with what you came up with to solve this problem. You ought to post that code as an answer to this question. – Sam M Feb 18 '16 at 07:03

2 Answers2

2

Finally, I used java.net.URL to get this working:

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

<!--- Input Stream --->
<cfset local.inputStream = local.objURL.openStream()>

<!--- Get Content --->
<cfset local.objScanner = createObject(
                              "java"
                            , "java.util.Scanner"
                          ).init( local.inputStream ).useDelimiter( "\\A" )>
<cfset local.fileContent = local.objScanner.hasNext() ? local.objScanner.next() : "">
Abhishekh Gupta
  • 6,206
  • 4
  • 18
  • 46
  • What about using [`IOUtils.toString(myInputStream, "UTF-8");` ?](http://stackoverflow.com/a/350723/104223). It is a bit simpler, and seems to be bundled with CF11. – Leigh Feb 18 '16 at 22:40
  • @Leigh Oh yes. Thanks for the info. :) – Abhishekh Gupta Feb 19 '16 at 05:43
  • "Apache" to the rescue :-) Though the scanner trick is pretty neat too, if you do not have access to the Apache lib. – Leigh Feb 19 '16 at 16:22
0

It is/was probably caused by missing certificate on your CF server for the https connection (keystore file: cf11/jre/lib/security/cacerts).

P. Furrer
  • 1
  • 1