I am using IdHTTP.Post in my application to upload data to a server in the cloud. A DLL in the cloud saves the data to a SQL server database, and returns a simple message back to the application.
The uploaded data is simply a bunch of long strings uploaded in a loop (won't go into why here). Each string in the loop is pretty much always the same length.
For example:
For I:=1 to 10 do
begin
...
Reply:=IdHTTP.Post(URL,String);
...
end;
This works perfectly well 98% of the time. Sometimes, when the loop is more than 100 say, then 'Reply' will be blank. If I try again, then it works fine.
I tried various things, one of which is to add
Sleep(2000);
after each iteration of the loop, this also seems to do the trick!
So, in short, my upload seems to be tying itself into a knot every now and then when the upload is on the large size. Any recommendations as to how to handle this better than 'Sleep' would be appreciated.
I have not made any changes to the IdHTTP component from the default settings, apart from:
IdHTTP.ConnectTimeout:=5000;
IdHTTP.ReadTimeout :=5000;
I am using Delphi 2010.
Note that the string lengths are the same for each iteration, whether I am looping 10 times, or 100 times. So the string length itself does not seem to be the issue.
Update
So ignore pretty much most of what I thought was happening above. @Remy Lebeau correctly identified that the problem was that the reply was not being decoded correctly, and that is because IdHTTPOptions.[hoForceEncodeParams] was set to false.
The reason for this is outlined in a previous question:
Delphi TIdHTTP POST does not encode plus sign
@Remy, my version of Indy, as you pointed out in the old question, and in response to this one, is certainly outdated. I was a bit hesitant to install a later version as the install instructions looked a bit scary, and so I opted to encode manually instead in the meantime. This looks to have backfired, so will look at it now for sure.
At least now I know what is causing my problem.
I have a few follow up questions that you might be able to help me with:
- I think I might be doing something wrong, but even if I set hoForceEncodeParams to True or False on the component, when looking at Fiddler, it doesn't always seem to be consistent in terms of the encoding going through to the server. I have only picked this up now because I'm looking at it in detail. The only way to be SURE of the encoding either way is to set it in the code right before posting, in other words ignore the setting on the component. Can this be correct, or what am I missing? This is why I understood the problem incorrectly in the first place, as the encoding was correct on the first iteration of the loop, but then inexplicably changed on the subsequent iterations.
- With a IdHTTP.Get, I DO get a reply even if hoForceEncodeParams is set to false. Does this make sense?