Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Problem with cfhttp in a loop

New Here ,
Apr 22, 2009 Apr 22, 2009

Hi,

I am reading an xml file that contains emails with attachments (one email per item/node, but may contain multiple attachments).  I am trying to save the attachments to our server.  So I'm looping over the multiple attachments of a single email and using cfhttp to GET the attachments from another server and save them with the same filename on our server.

The problem is that it works for the first attachment - it's saved to the server, status code is 200 OK - awesome!  But for any attachments after that, it does not save them and throws a Conection Failure error.  No matter what attachments they are.

In trouble shooting I tried several things.  First, inside of my loop of attachments, I can hardcode the cfhttp calls with the url and filename of the attachments - one right after the other - and all is perfect everytime!!  But it obviously needs to be dynamic.  I also tried to save a list of the attachment urls from the loop, and then call a separate cfhttp tag for each attachment in the list (so again, was in a loop) and it works for the first attachment in the list and not for the others (same errors as above).

Here's a simplified version of the code.  I can't put in the real xml url, and when I set the "attachmentFilename" I left out that code because it works and is too much code.  Also, the XML works fine.  Please let me know if you have any suggestions, and of course, if you need more info from me!!

Thanks so much,
Kirsten

<cfoutput>

<cfhttp url="https://www.myxml.com/example.xml" method="get" resolveurl="no" />

<cfset myXML = trim(cfhttp.FileContent)>
<cfset myXML = xmlParse(myXML)>
<cfset theRoot = myXML.XmlRoot>

<cfset numChildren = arrayLen(theRoot.XMLChildren[1].XmlChildren)>
<cfloop index="i" from="6" to="#numChildren#">
    <cfset attachments = theRoot.XMLChildren[1].XMLChildren["attachments"].XmlText>
   
    <cfif ListLen(attachments, "|^|") gt 2>
        <cfset loop_unid = theRoot.XMLChildren[1].XMLChildren["unid"].XmlText>
        <cfset counter = 0>
        <cfset attachmentArray = ListToArray(attachments, "|^|")>
       
        <cfloop from="1" to="#ArrayLen(attachmentArray)#" index="k">
            <cfset counter = counter + 1>
            <cfset attachmentURL = attachmentArray>
            <cfset attachmentFilename = Replace(attachmentArray,"strip the url from the filename in the url","")>
            <cfhttp url="#attachmentURL#" method="get" resolveurl="no" timeout="120" path="D:\my_servers_path\attachmentFolder\" file="#attachmentFilename#">
           
            attachment counter: #counter#<BR />
            cfhttp.statusCode: #cfhttp.statusCode#<BR />
            cfhttp.errorDetail: #cfhttp.errorDetail#<BR />
           
        </cfloop>
    </cfif>
</cfloop>

</cfoutput>


Output Results:

attachment counter: 1
cfhttp.statusCode: 200 OK
cfhttp.errorDetail:


attachment counter: 2
cfhttp.statusCode: Connection Failure.  Status code unavailable.
cfhttp.errorDetail: I/O Exception: peer not authenticated


attachment counter: 3
cfhttp.statusCode: Connection Failure.  Status code unavailable.
cfhttp.errorDetail: I/O Exception: peer not authenticated

1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 22, 2009 Apr 22, 2009

You can leave the file attribute off, and it will attempt to save the file with the filename as specified in attachmentURL

"For a GET operation, the default is the file requested in the URL, if there is one. For example, if the URL in a GET method is http:www.myco.com/test.htm, the default file is test.htm."

not sure that's your problem though.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 22, 2009 Apr 22, 2009

We've seen the peer not authenticated error quite a bit on our CFHTTP calls - are you trying to access the URLs via HTTPS?  It seems the SSL certificate keystore that CF ships with does not contain all the vendor SSL certificates out there - when CF can't authenticate a cert, it throws that "Connection Failure / Peer not authenticated Error".  In order to fix the issue, you have to import the SSL certificate of the site you are trying to access into the CF cert store on your server(s).  If you are using multiple servers, you will have to import the cert on each server.

I'm sure a google search will turn up a step by step guide on how to do this, but the basic steps are:

  1. Go to a page on the SSL server.
  2. Double-click the lock icon.
  3. Click the Details tab.
  4. Click Copy To File.
  5. Select the base64 option and save the file.
  6. Copy the CER file into ColdfusionDir\runtime\jre\lib\security
  7. Run the following command from the same directory (keytool.exe is located in ColdfusionDir\runtime\jre\bin) ..\..\bin\keytool.exe -import -keystore cacerts -alias UniqueName -file filename.cer
  8. Restart Coldfusion

Hope that helps!

- Michael

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 22, 2009 Apr 22, 2009

Thanks for both of the replies -- it's greatly appreciated!!

The reason I don't think either of these would be the problem is because it WORKS with the first attachment.  All attachments are from the same server (and yes, they are https).  Could this still be the problem if it does work at least one time?  Wouldn't that error prevent it from happening at all?  And when it's hard-coded, those exact URLs work -- https and everything, no issues.

I'm just so baffled by this!!!  Ugh!  Any more thoughts?!?!

Thanks!!!

Kirsten

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 30, 2009 Apr 30, 2009
LATEST

My coworker solved this!  The problem was that, apparently, the list I was making of attachments had some extra spaces in it -- which didn't cause a problem for the first list item, but all others had the space, which threw the Connection Failure error.  So a simple trim() around the list item inside the loop makes it work!

YAY!!! 

Thanks for the responses!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources