Download File from URL within For Each loop throws error

Facts:

I am attempting to download pdf files from URLs. The files will automatically download upon clicking, or entering the URL. Unfortunately, I am unable to share the specific links due to client security & privacy; however, I have confirmed that the links work when individually entered into the Download File from URL activity.

Problem:

The problem arises when I attempt to loop through the list using a For Each activity with the Download File from URL activity in the Body section of the loop. The first URL will successfully download, but when it comes time to download the second file, I am met with the following error:

Download File from URL: Cannot access a disposed object. Object name: 'System.Net.Http.HttpClient'.

Sequence Structure:

I have attached a screenshot of the sequence below (this is part of a much larger workflow, but when I was troubleshooting I noticed it still threw the error at the smallest level so I figured for simplicity this would suffice)

Exception Information

I have entered the exception information below to help others with any future searches.

$exceptionActivityInfo

“Id”: “1.6”

“InstanceId”: “7”

“Name”: “Download File from URL”,

“TypeName”: “UiPath.Activities.System.FileOperations.DownloadFileFromUrl”

$exceptionDetails


RemoteException wrapping System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Http.HttpClient'.

at UiPath.Core.Activities.TaskAsyncCodeActivityWithResult`1.EndExecute(AsyncCodeActivityContext context,

IAsyncResult result)

at System.Activities.AsyncCodeActivity`1.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,

IAsyncResult result)

at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,

BookmarkManager bookmarkManager)

Edit Note:

I also tried testing this problem in Web Studio and the problem persisted there as well.

@Jsorey04

Introduce a little delay inside the loop and check..generally this error occurs when a disposed object is being used again or in asynchronous runs..so adding a delay might suffice

Alternately try to move the output file somewhere so that the link is broken

Cheers

Hi @Jsorey04

Try to include some activities before and after download activity.

Before:
Add log message activity to print the current value of the file that’s going to be downloaded.

After:
Use file exists activity to check whether file has been downloaded or not? If yes, print log message stating file has been downloaded successfully.

Such steps also aid in troubleshooting. If still doesn’t work, induce a delay specifically in between downloads.

Regards
Sonali

Hey @Anil_G, I appreciate you taking the time to respond! I tried adding in the delay (set for 2 seconds) and it did not work. I added in the Move File activity and it did not work either. I also tried a combination of the two activities, and that also did not work…

I ran the process through a Python script in VS Code and it worked so I might end up creating a Python Scope sequence, but it’s annoying to have to essentially restart from there after working on this project for a few weeks.

Hi @sonaliaggarwal47, I appreciate your response!! I have this activity in a much longer sequence, but I figured for simplicity breaking out in the smallest section possible could be beneficial. However, I stand corrected! I tried Anil’s (and your) suggestions and the error persists. If you think a full screen shot of the sequence would be beneficial please let me know and I’ll see what I can do. I can also upload the Output logs if you those would be of assistance.

@Jsorey04 A walkaround I could think of is to add retry scope to download file from URL activity. Lets keep number of retry to 5 and interval to 5 or 10 seconds. Can you try this If its helpful ?

thanks!

Hi @Jsorey04,

How about surrounding this by try catch block?

Whenever this exception occurs, bot would catch the exception, and in catch block, there will be download file from url activity which would try to download the item again.

You should be using the Wait for Download activity so that the loop does not continue until each file is downloaded.

1 Like

I tried this method before I posted, but I tried it again just to be sure. It immediately stops when the error appears.


Hi @Jsorey04

Yes it would stop as soon as error appears. That’s why I have suggested to use try catch instead.

It will catch the error and try to download again.

@Jsorey04

instead of debug use run ..it debug it would pause on error and if you click continue it would retry again

cheers

Use Wait for Download so the loop doesn’t continue until the file is downloaded.

@sonaliaggarwal47
As with Darshan’s suggestion, this was one of my previous troubleshooting items on Friday before posting. This time for the Catch I tried all the following methods (since there there is no System.Net.Http.HttpClient catch)

  • System.Exception
  • System.Net.Http.HttpIOException
  • System.Net.Http.HttpProtocolException
  • System.Net.Http.HttpIRequestException

The errors still occurred…


Hey @postwick, thank you for joining this conundrum. I thought your recommendation was just going to keep the process waiting indefinitely; however, interestingly enough it still ends up throwing the error.


@Jsorey04 In debug mode it will stop the process when error encounter you have to click on continue to run the process as it will retry to download the file. Like wise it will retry. Can you try this?

@Anil_G, ahh okay. I see now. Here is after running the workflow instead of debugging.

I think I’m not understanding how the link is being disposed of when I have it manually typed in the list it’s being pulled from.

Edit: Sorry @Darshan_Sable was the one who recommended the Retry Scope activity. I’ll try the Delay and Move File activities and get back to you.

Hi @Jsorey04

Error would still be there that’s when it it would enter catch block and redo the stuff. But as per your logs, I don’t see it entered catch block.

Did it get caught by any other catch block and tried to download again.

Also, please run it not debug.

@Jsorey04

One try you can do is to reitialize the output variable at the start of for ..so that for each iteration the variable would be fresh

Cheers

@Anil_G,
As promised, here is the Screenshot from the error thrown this time.

@Jsorey04

try this to reinitialize

before download use assign with

urldownload=LocalResource.FromPath("")

or set to Nothing

cheers