send xml data to server and get response - iphone

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.

Related

NSURLConnection receives 'False' on file download

I am trying to download a file from a server to an iphone like this
NSURL *newURL0 = [NSURL URLWithString: url];
NSMutableURLRequest *req0 = [NSMutableURLRequest requestWithURL:newURL0];
NSURLConnection conn = [[NSURLConnection alloc] initWithRequest:req0 delegate:self];
and then
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSURL *docDirectory = [[[NSFileManager defaultManager] URLsForDirectory:
NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *filePath = [docDirectory URLByAppendingPathComponent:temp];
[webData writeToURL:filePath atomically:YES];
[webData release];
connection = nil ;
if ([delegate respondsToSelector:#selector(getDataSucceeded:)]) {
NSLog(#"Log: %#", #"yes");
[delegate getDataSucceeded:self.list];
}
}
This always worked perfectly, but recently I have noticed that it does not work anymore. No changes were made to any files relevant to this function, it just kind of stopped working. when I use the same url in a web browser the file downloads fine, but in my app when I request the file it just returns 5 bytes, the word 'False' which I also found strange because the server does not return the word False, if there is an issue it returns a error message. The server side code is written in Visual Basic.
Any thoughts or suggestions are much appreciated
False is a keyword in Visual Basic, it is not a keyword in Objective-C. There's also no reason for that to appear as text in a response like that. It's almost certainly being generated server-side.
Use an HTTP proxy such as Charles to see what requests are made by your web browser and by your application. There will be a difference between them that is causing the server to respond in a different way. Use a tool like cURL to make requests manually to determine which of the differences is the cause. Alternatively, check the commit log for your server-side code for recent changes that could cause this.
i think you missed the following call back function in which you need to append the chunck of data in a single NSData object :
in my case _responseText is a NSData object
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseText appendData:data];
}
Hope this will help you out

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");
}

webView and 'timeout'

I have question, in my application I need to have a connection and a webview to display the web content.
The problem is that I have a username and a password which need to be right in order to get a connection. Now I want to have an alert that comes up if the username is wrong. The
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
does not work for this, because if the username or the password is wrong it will continue loading (even though it won't display anything).
Is there a possibilty to 'tell' webview to set a time interval and then display an alert?
THANK YOU SO MUCH!
If you're authenticating with your server via HTTP authentication (per your comment above) then I would do this differently. When the user enters their username/pwd, make a request to any page (something lightweight) using NSURLConnection. If the username/password are incorrect you can handle the failures in the NSURLConnectionDelegate methods and prompt your user for a new username/password.
You can also do this with the iOS 5 version of NSURLConnection instead of the delegate methods but the docs aren't very good.
When this first request succeeds then load the web view.
EDIT: added example of using NSURLConnection to check if HTTP auth succeeds
Ok, here's a simple example. You can make this better by allowing NSURLConnection to load your intended request and if it succeeds load the response data into the webView vs initiating a new request. Read the URL Loading System docs to learn more about how to use NSURLConnection.
In your webView delegate, initiate an NSURLConnection with a request to the same domain as the webView request with the user credentials as follows:
- (void)performHTTPAuthRequest
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://%#:%##test.starsightings.com", username, password]]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
// if the request succeeds, credentials must be good so load the webView
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// request completed successfully so username/password must be good
[self.webView loadRequest:theRequest];
}
// ignore the failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
// if we get an auth challenge, the credentials are bad so cancel the request and alert the user
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[[challenge sender] cancelAuthenticationChallenge:challenge];
// alert user that username/pwd is invalid
}

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.