NSMutableURLRequest timeoutInterval in iOS - ios

I am trying to adjust the timeout on a NSMutableURLRequest for a simple HTTP GET using setTimeoutSeconds:, but not having any luck. It seems to work for any values under 60.0, but if I set a larger value (ex: 90.0), then it still times out at one minute. I've tried setting it to 0.0 hoping it would have no timeout, but no luck there.
I have read many posts on this subject, some about POST issue which doesn't apply, but never seen a solid answer.
It seems to be a bug in iOS but I can't see anyway around it. I don't want to use ASIHTTPRequest, I need a workaround that I can use with NSMutableURLRequest.
Can anyone please help?

Related

User Default Values Changing to Previous Values Seemingly Randomly - Swift

I am creating an app that is a game where there is a need to store the high score. I believe (correct me if I'm wrong on this, but this question made it seem this way) that user defaults are the best way to accomplish this.
Sometimes when I switch between views in my app, the values change to what they previously were. Then I will switch back to another screen, and they will go back to what they should be. This happens kind-of hit and miss, so it is hard to tell, but I think it happens most after it has been set multiple times since the app was last closed; though, I know at least sometimes it only happens after they have been set once.
I have used breakpoints to ensure that the values definitely are not being changed in the code. (I did this by putting a breakpoint where all of the user defaults are set and none of them went off even though the values changed)
The only code dealing with the user defaults is as follows:
UserDefaults.standard.set(newHighScore, forKey"highScore")
-> To set the User Defaults (where newHighScore is an integer)
let highScore = UserDefaults.standarard.value(forKey: "highScore")
-> To retrieve the values
UserDefaults.standard.synchronize()
-> Every time before and after I set or retrieve values (just to play it on the safe side and because it doesn't work so I'm overly cautious)
This question suggested restarting my mac (which I did) and then doing it only in the xcode simulator. I just did that and kept the breakpoints on all the times I set the values.
I have looked at these questions and this question seems similar, but the problem doesn't apply because it was decided that it would be fixed if the code wasn't running in a playground. My code is not running in a playground.
This question also seems similar, but it doesn't apply because the values are returning as null. Mine are just changing to a value they were previously. It is also written in objective-c, so I didn't completely understand what they did to fix it. I looked through the sources it recommended for help, but didn't see anything there that helped me either.
Thanks in advance for any help on what may be my problem. I haven't been able to find anything similar to make it seem like a bug in iOS 10 or Xcode 8 beta, but I am open to any suggestions to make my code work.

Indy IdHTTP.Post intermittently not returning reply in Delphi application

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?

Possible encoding mismatch from iOS Client

I've got an iOS application, and we've been getting some errors that seem to be related to encoding problems because some of content is getting truncated when it reaches the server. This is problematic because it's been messing with our authentication mechanism, which involves making a hash of the posted content (along with some other stuff, but that's the basics).
So far we haven't been able to reproduce the problem locally. We have a theory that it's related to something like, an emoji keyboard; but I've tried posting emojis, international characters, everything that I could think of. Everything gets posted correctly when I try it. I know that there was a difference in the way ios encodes emoji between ios 4 and 5; we've managed to speak with a few of our customers to try to get an idea about what they're using, and it's usually iOS 6, with either an iphone 4s, or 5 (just like our test devices, I've tested with ios 5 and 6) ... so it wouldn't seem to be related to the OS.
This is how we are setting the request's body for the post.
[req setHTTPBody:[paramString dataUsingEncoding:NSUTF8StringEncoding]];
Previously, I was just setting the content-type to this:
[req setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
And my theory is that being explicit about the charset in the content type might fix it:
[req setValue:#"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
So my question for you SO, is this crazy? do you think being explicit in setting the charset to UTF-8 would resolve the issue? Any other troubleshooting techniques that you can recommend to figure out where the problem is occurring?
Do you have a wireshark or fiddler trace of a failing request? I would start there to verify that the client is indeed sending truncated data, and it's not a server side issue.
Check that the Content-Length header is correct regardless of data sent. Since the issue is not easily reproducible, perhaps collect more specific data from users who saw the problem, so the problematic inputs can be identified, and the broken request can be reproduced repeatedly.
I doubt it's an encoding issue if you are pulling strings straight from a UITextField, you shouldn't have encoding issues. If you are going to/from CStrings or NSData then that would be the first place to check.
Not a conclusive answer by any means, but that's where I would start.

withContentsofURL possible to declare a timeout limit?

I have an app with dynamic data and the update method uses arrayWithContentsofURL and dictionaryWithContentsofURL to get the plists from a server in order to update my database.
My problem:
When there is no or not correctly working internet connection on the device this request simply tries to get the data for about a minute before it stops trying and continues execution.
Is there a way to maybe set a timeout for this function?
PS: I know this is probably the worst way to do this and I would be happy if someone could point me in the right direction :) I'm quite new to iOS programming so please be patient.
In my opinion it's best to use an NSMutableURLRequest with it.
Which has a - (void)setTimeoutInterval method. From the documentation:
The timeout interval, in seconds. If during a connection attempt the
request remains idle for longer than the timeout interval, the request
is considered to have timed out. The default timeout interval is 60
seconds.
Suggest you use an NSURLRequest to send the Request object. Its delegate functions will return you the plist.
You could take this example, about half way on that page it downloads a json object very much the same way as you could fetch a plist.

How to limit size of string in stringWithContentsOfURL:encoding:error:?

I do not see any answer on similar question regarding limiting size of read string from URL. A user can point by mistake on URL bringing several gigabytes of data which obviously will bring down my iPhone application. I want to limit size to 100Kb, is there any way to do that using the standard method?
In short, no, because the method you're referring to is pretty much a convenience method. If you want to check the size of the request prior to making it, you might as well use ASIHTTPRequest or even just NSURLConnection.
Specifically with ASIHTTPRequest, you'll get a delegate callback when the HTTP headers are received, so you'll know the size. Then, if the size is 100KB+, you can just cancel the request.

Resources