Which is the best way to handle request-timeout. I am sending XML request and if I didn't get response in 10 sec I need to stop activity indicator and show alert message.
Which approach is best out of following:
a. NSTimer - To check status of response
b. NSThread - This will run in background to check response
c. Notification class (I never used it)
Thanks
If you use NSMutableURLRequest then you can give time interval in this
for eg.
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
so no need to use timer or thread
Related
As soon as, I send request to the server (via NSURLConnection sendSynchronousRequest method), the server receives it in about 2 seconds, it processes and sends back response in another 3-5 seconds. However, I only get back the response in 30-35 seconds. This delay makes our communication very slow.
Even the async APIs are getting a delayed response.
Earlier, everything was working fine, with client getting the response back within 10 seconds.
Anyone else having this issue? What could be the reason?
EDIT
here is a screenshot of Wireshark analysis:
Link to a better image
How should I see what packet is saying what?..and why is it getting delayed?
EDIT2
Here is the code:
NSHTTPURLResponse *response=nil;
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:nsURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setTimeoutInterval:180.0];
[theRequest setHTTPBody:[[NSString stringWithFormat:#"%#",sdata] dataUsingEncoding:NSASCIIStringEncoding]];
NSError *error= nil;
NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
if (error ) {
NSLog(#"error sending synchronous request: %#", error);
}
NSLog(#"request completed with code:%d",response.statusCode);
I'd avoid calling sendSynchronousRequest. Use the asynchronous version instead if you're not doing the call already on a background thread (you don't want to block the UI thread).
How do you know when the iOS response is received? An NSLog? A UI state change?
See also these questions:
NSURLConnection sendSynchronousRequest - background to foreground
NSURLConnection sendSynchronousRequest taking too much time to respond
Update
If you're a bit stuck, one strategy might be to rule out the use of NSURLConnection as the problem.
Strategy 1: try using NSURL's asychronous connection call instead of synchronous
Strategy 2: try using a different HTTP lib, such as AFNetworking
If you want to take a closer look at what is going on with the HTTP connection, you can use tools such as Charles, Fiddler or Wireshark to debug what data is being sent and received. To get the most benefit from this sort of analysis, you need to have some knowledge of the HTTP protocol(s). This is probably more time consuming than the previously mentioned strategies.
See also questions such as How to monitor network calls made from iOS Simulator.
Update
Are you accessing a webserver of your own, or is it someone else's?
Have you had a close look at the headers being sent to your webserver (and the ones being returned)? Pay attention to the content length, for example. Wrong content length can cause a delay, as explained here.
To see the request and returned headers, you could use Firebug, or something like wget or curl on the command line.
Also, double check that there's not a newline on the end of your URL, as described here.
Solution that worked for me:
In the request headers, iOS sets "gzip" for "Accept-Encoding" by default. The gzip compression was taking a lot of time, and hence the delayed response. I did the following to solve the problem:
[theRequest setValue:#"" forHTTPHeaderField:#"Accept-Encoding"];
NOTE: Check your headers, for any response delay.
Thanks to #occulus for directing me to the request headers!
You do this over wifi or celluar?
Speaking from my experience, when my data useage is exeeded my phone-provider slows down my downloads. Sometimes this happen to me at the end of the month after I did use a lot of mobile data.
I am not sure to say but it may be network problem first check it. it proper or down ?? okay Maybe I will be wrong.. but first check it...
Following describe code it might solve your problem :)
NSString *url = #"Your URL ";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
.
.
/// Add Here Your NSMutableData Valuew
.
.
[request setHTTPBody:body];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
{
self.responseData = [[NSMutableData alloc] init];
}
else
NSLog(#"Connection Failed!");
To ease your problems with asynchronous HTTP Request you should consider using the AFNetworking framework
I am wondering what the difference between Get and Post with asihttprequest library..
Is this a GET?
- (IBAction)sendHttpsRequest
{
//Set request address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:#"https://142.198.16.35"];
//call ASIHTTP delegates (Used to connect to database)
NSURL *url = [NSURL URLWithString:databaseURL];
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
is a post when you try to set elements say within a php document? any examples would be awesome!
http://www.cs.tut.fi/~jkorpela/forms/methods.html
An HTTP GET is a request from the client to the server, asking for a resource.
An HTTP POST is an upload of data (form information, image data, whatever) from the client to the server.
What you have there is an HTTP POST.
-EDIT:
Per http://allseeing-i.com/ASIHTTPRequest/:
ASIFormDataRequest
A subclass of ASIHTTPRequest that handles x-www-form-urlencoded and multipart/form-data posts. It makes POSTing data and files easy, but you do not need to add this to your project if you want to manage POST data yourself or don’t need to POST data at all.
My bad, this one was a POST, not a GET. The rest of my answer was valid, though :)
That is a POST request, which is the default for ASIFormDataRequest. The difference is the same as it would be in a normal HTTP request. You can read about that here if you don't already know.
In general, if you are just downloading a web page and do not need to send any variables to the server, a GET request is sufficient. If you want to send variables in your request, often times a POST request is the way to go since it is a bit more secure and less transparent.
iam using to download multiple file using by pass ASIHTTPRequest to operation queue...
NSInvocationOperation *operation =[[NSInvocationOperation alloc]initWithTarget:self selector:#selector(DownloadFile:) object:url];
.
.
.
-(void)DownloadFile:(NSURL)url{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:self];
[request setDidFailSelector:#selector(requestWentWrong:)];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDelegate:self];
[request startSynchronous];
}
- (void)setProgress:(float)progress{
NSLog(#"Current progress %f :",progress);
}
The progress is working fine but i can't know from which URL or from which operation..
I want to know how to get each download progress of each url individually...
and how i cancel each one not cancel all operations.
Thanks
First an answer to your question: how about you create one "delegate" object for each download? Then it's obvious how download progresses for each download and cancel is easy, too. The code will be more complicated, though.
Therefore I have another suggestion: the author of ASIHTTPRequest library has stopped developing the library, so you might switch to something else. He's suggestion for example AFNetworking, but many people recommend nowadays MKNetworkKit. It seems to have pretty good queue handling.
Note the signature of the delegate messages: Each one takes an argument. That argument is the request sending you the message: The request, when it sends you a delegate message, includes itself among the arguments so that you know which request has gotten that far.
I am doing an application in which I am uploading the images to twitpic. It works fine the first time. But if I try to upload the next image within that minute itself, it shows a 401 error. If I try again after waiting for a minute, it gives a 200 response and works fine. Why is this happening? Can I send images continuously to twit pic without any interruption?
Well the documentation of TwitPic states that there is 500 call limit on the API.
And the 401 will happen only if you do supply the correct Auth header.
My guess is that there is something wrong with the auth header, it might be due to caching.
If you use NSURLConnection nochache paramter:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
I am sending a request with POST data to a web server. The web server returns with JSON response in case of an error or the file data itself if there are no errors.
I would like to track the progress of the file data response. My code is based on the the sample code from ASIHttpRequest Tutorial
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:someValue forKey:#"someKey"];
[request setPostValue:someOtherValue forKey:#"someOtherKey"];
[request setShowAccurateProgress:YES];
[request setDownloadProgressDelegate:aProgressView];
request.delegate = self;
[request startSynchronous];
Nothing happens until the complete response is there, in which case the progress bar fills up completely.
I experimented with both synchronous and asynchronous requests.
I guess the download progress delegate does not work, because I am not downloading a file per se, but just receiving the HTTP response to the request, right? What would be the correct approach to monitor the progress in this case?
Thanks in advance...
Are you running this code in the main thread?
If so the reason the progress doesn't update is that using a synchronous request will be blocking the main thread, preventing UI updates from happening.
The best fix for that is to use an asynchronous request - you mentioned you've tried that, what happened?
Tip:
Print http response and check if you have Content-Length. If not, or it's 0 that's the problem.
NSLog(#"Response headers %#",[request responseHeaders]);