NSURLConnection basic authentication issues with didReceiveAuthenticationChallenge - iphone

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.

Related

Timed out error for NSUrlRequest

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?..

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..

Using gzcompress in PHP; need to be able to uncompress on iPhone

I'm sure this isn't too difficult (and I'm surprised I can't figure it out), but here goes:
So. I'm using gzcompress() to compress JSON data in PHP so I can send it into an iPhone app.
Having some troubles figuring out how to uncompress this data on the iPhone (iOS).
Any thoughts? I'm grabbing the data via a NSMutableURLRequest.
Thanks!
Code making the request:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Code processing the response:
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
And on the PHP side:
echo(gzcompress(json_encode($lines)));
Thanks!!!
This article suggests that NSMutableURLRequest supports gzip decompression out-of-the-box, but you need to add an Accept-Encoding: gzip header to the request.
Extending your example:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
I'd also recommend testing the PHP script with cURL to make sure it's sending valid gzipped data.

send xml data to server and get response

i am making an app in which user's data is send to a server...the data should be in xml format.
presently i have made a string and put all into it...like following format
NSString *s=[[NSString alloc]initWithFormat:#"%#%#",name.text,address.text];
(this is just an example i have made a string with full xml tags including xml version tag)
and then send this through http post method....
i did it but dont know how to get response of server...please help ...any code will be helpful.....
waiting for answer
Take a look at NSURLConnection. You essentially create a connection, register a delegate, kick off the request and build the response as the data is passed to your delegate.
I'm working from memory here, but essentially:
Create an NSURLRequest for your request to your server
Create an NSURLConnection using the initWithRequest:(NSURLRequest *)request delegate:(id)delegate init method, passing a suitable delegate.
The request will be made, and the response will be passed back to your delegate in:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
I've not tested this, but something like:
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"SERVER"]
[request setHTTPMethod:#"POST"];
[request setHTTPBody:#"Your XML"];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Then in your delegate you can build the response using the data provided in the didReceiveData:(NSData *)data and didReceiveResponse:(NSURLResponse *)response methods.

iPhone and ASIHTTPRequest - Unable to start HTTP connection

I am using the ASIHTTPRequest class in order to communicate with a web service and get a response. This is how I send a request to the server
NSString *str = [NSString stringWithFormat:#"%#verifyLogin.json", serverUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request addRequestHeader:#"Content-Type" value:#"text/x-gwt-rpc; charset=utf-8"];
NSDictionary* data = [NSDictionary dictionaryWithObjectsAndKeys:emailId, #"username", pwd, #"password", nil];
[request appendPostData: [[data JSONFragment] dataUsingEncoding: NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setDidFinishSelector: #selector(gotLoginResponse:)];
[request setDidFailSelector: #selector(loginRequestFailed:)];
[networkQueue addOperation: request];
[networkQueue go];
The control immediately goes to the error routine and the error description and domain are
Unable to start HTTP connection and ASIHTTPRequestErrorDomain
I can get the same request to work via a desktop tool for checking HTTP requests so I know the settings are all correct.
Can someone please tell me what I am missing here while sending the request?
Thanks.
As I commented earlier;
in your first line it says
"%verifyLogin.json". Shouldn't that
be; "%#verifyLogin.json" ????
For me I forgot to add http:// as i was testing locally. But after adding http it ran successfully
Can you check the status code of the request when it fails? I recently dealt with a JSON user authentication issue with ASI-HTTP-Request where the first request would always fail with Status Code 0 and some sort of underlying network connection failure (as if it wasn't connected to the internet, but clearly was).
I was able to solve it by setting that particular request to not attempt to use a persistent connection.
So try that first!