Timed out error for NSUrlRequest - iphone

When I open the app from background I need to hit server to get some data. When I'm doing so I am getting an alert as follows:
"Request timed out" (nserror's localised description)
I'm on wifi and my internet as well as my server are fine.
This is not happening every time but happening frequently. Here is my code:
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:myUrlString]];
NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self]; //sending request for data self.dataConnection=conn; [conn release];
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error" message:#"Network Exception" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
What am I doing wrong and how can I fix this?
NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
even i've used the above line instead of
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:myUrlString]];
but found nothing different . Still my request is getting timed out. Why is it getting timed out even when my server aswell as wifi (internet) are fine.?? Thanks in Advance....

NSURLRequest *lRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
From here you can get the guidance...NSURLConnection timeout?

If you're confident of your server-side then it may be not a software, but a hardware-specific issue. I have this issue very often with my iPod5 connected to home wifi, despite all other devices connected to the same wifi are fine. Today I have tested connection to my server on problem device via another wifi - timeout errors were disappeared. Weird. Who you gonna call?..

Related

iOS check server connection and successful download

I'm making an app that downloads data from a plist and uses that data in a tableview. Is there a way to check whether a connection to a url is successful and then whether the download was successful? Or maybe a tutorial that implements something like this?
Thanks
you must implement this delegate of NSURLConnection
//send the request
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
in the case of connection failure,this method will be executed
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSlog(#"no connection");
}

ASIHTTPRequest with iPhone 3G

I am using ASIHTTPRequest in my project and i have a problem with iPhone 3G.
i send a request to :
NSURL* pageURL = [NSURL URLWithString:#"https://accounts.google.com/ServiceLogin?service=sj"];
loginPageRequest = [[ASIHTTPRequest alloc] initWithURL:pageURL];
[loginPageRequest setTimeOutSeconds:30];
[loginPageRequest setDelegate:self];
[loginPageRequest startAsynchronous];
and in other devices i get a responde to :
- (void)requestFinished:(ASIHTTPRequest *)request
and in iPhone 3G i get a lot of times :
- (void)requestFailed:(ASIHTTPRequest *)request
with error MSG : The request timed out
i set the the request timeout to 30 seconds.
any idea why it happen?
I also had this problem on iPhone 3G!
I figured out it was a HTTP*S* (SSL) problem. My "valid" certificate was flagged as invalid.
You might add [request setValidatesSecureCertificate:NO]; to your request.
If you are lucky you have the same problem. :)

Check web server is up or down in iphone

I need a little help here!!!
If string 'url' contains the address to web server, how do i check whether my web server is up or down?
I got some codes to check Internet connectivity. But even if we have internet connectivity how do we know server is up or not? Is there any better way to do that?
Thank You
You could do a request,
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest
delegate:self];
Now if you implement (along with all other delegate methods)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
you will get either succes or error.
Post a request, get the response. If you get a response, the server is up, if not its down..

NSURLConnection basic authentication issues with didReceiveAuthenticationChallenge

Running into some weird issues with didReceiveAuthenticationChallenge. If I do something like:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:WAF_URL]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60];
[request setHTTPShouldHandleCookies:NO];
self.conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
I receive - (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge challenge which if successful, triggers connectionDidFinishLoading.
But, if I try to do a post with the request, I seem to be running into issue where my connectionDidFinishLoading is never triggered and my request times out.
This seems to be an issue only on iPhone and not on the simulator??!
My server was using NTLM challenge, which when used with Post with NSURLConnection screwed up. Removed that and it's fixed.

iPhone: How to download a raw .config file via program?

I am trying to download a xyz.config file which is created using iPhone Configuration utlity, via code.
NSString *urlString = #"http://xxxxx:xxx/ms/servlet/ConfigServer?userid=xxx&pwd=xxx&emailid=xxxxx";
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
Then, didReceiveData, didReceiveResponse are written. Finally,
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(#"Response : %#", responseString );
// release the connection, and the data object
[receivedData release];
[connection release];
}
With the above code, i'm getting response as string of the complete configuration file. But my Aim to download this file as raw data, which will automatically launch device profile to ask for installing this profile.
NOTE: I am able to provide the same URL in Safari browser on the device, and download the
raw file directly to install it on the device. I don't want to use Safari browser to download it, it should be done via my communication code.
I also tried with ASIHTTPRequest like below, but unable to download that file directly from the URL via this code as well.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"GET"];
[request startSynchronous];
Please help!
Thank you.
The Mobile Safari browser app on the iOS has a lot more freedom than your app. Case in point, on the latest iOS 4.3, Mobile Safari has the Nitro JavaScript engine which boost 2x performance increase. But the UIWebView which is available to your app doesn't have that engine. Your app is in a sandbox, therefore, you cannot write outside of that box.