Home > Articles > Programming > Web Services/ XML/ SOA/ WebSphere/ WCF

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowMarcus Zarra

Marcus Zarra 

Learn more…

Basic Animations with Core Animation
Dec 28, 2009
Marcus Zarra and Matt Long on Core Animation
Jun 5, 2009
Introduction to Migration in Apple's Core Data
Mar 7, 2008
A Look at Apple's Core Animation
Feb 1, 2008
The State of Java Development on OS X
Jan 4, 2008
Packaging Your Application for OS X
Nov 21, 2007
Tiger vs. Leopard: Which Should You Develop For?
Oct 26, 2007
Building on Apple Sync Services
Sep 8, 2006
Getting in Sync with OS X
Sep 1, 2006
Syncing Your Data the Cocoa Way
Aug 11, 2006
Controlling Both Ends of the Communications Channel: From Cocoa to Servlets
Jun 2, 2006
Cocoa: Working with XML
May 19, 2006
Java EJB 3.0: A Hibernate Clone?
Apr 28, 2006
Document Level Parameters Using Core Data: A Guide for Cocoa Developers
Apr 7, 2006
Tips for J2EE Development
Feb 24, 2006
The Cross-Platform Java GUI: Designing Code for More Than One Operating System
Feb 10, 2006
Avoiding Java Exception Abuse
Dec 22, 2005
Java Perspective: Advanced Core Data
Dec 16, 2005
Java Perspective: Cocoa-Java Bridge
Dec 1, 2005
Java Perspective: Core Data and the Mac OS X Developer
Nov 23, 2005
Java Perspective: Key-Value Protocols, Behind the Magic of Mac OS X Development
Nov 11, 2005
Java Perspective: Cocoa Subclasses and Delegates
Nov 4, 2005
The Java Perspective: Cocoa's Interface Builder
Oct 21, 2005
Maven: Handling Multiprojects
Sep 23, 2005
Java, Maven, and Your Web Development Project
Aug 26, 2005
Ant User, Let Me Introduce Maven
Jul 29, 2005
Using JAAS Authentication with JBoss
Jun 17, 2005
Building a Custom JBoss Login Module
Jun 10, 2005
Testing Java Enterprise Applications with Cactus
May 20, 2005
Ant: Building a Better Build File
May 6, 2005
XDoclet: Entity Bean Relationships
Apr 8, 2005
The Java Developers' Introduction to XDoclet
Jan 7, 2005
An Introduction to Cactus
Dec 30, 2004
Using Multiple Databases with JBoss
Nov 24, 2004
JBoss Application Configuration Breakdown
Nov 5, 2004
Sortable Swing JTable
Oct 1, 2004
Building a Professional Swing JTable
Sep 10, 2004
Creating a Custom Java Swing Tablemodel
Aug 27, 2004
Adding Workflow Control to Your Java Applications
Mar 26, 2004
The Mac Developer Network
By on November 21, 2007 No Comments
Steve Scott of Late Night Cocoa has started a new podcast targeted specifically at Macintosh Developers.  One of the first episodes on this podcast is a roundtable of developers with several very well known developers starting with Daniel Jalkut, Uli Kusterer and Fraser Spiers along with myself.
Once more unto the [Beta Cycle], dear friends, once more
By on November 16, 2007 No Comments
Today marks the first semi-public release of iWeb Buddy, a new application that I have been developing for the past few months.  Unlike seSales or applications developed as a consultant, this development cycle has been a blur of activity.
Apple -- You Did Me A Disservice
By on October 26, 20073 Comments
Today is the day that most Mac users have been looking forward to for over a year.  Ever since Steve Jobs got up on stage last August and showed us Leopard for the first time.  Ever since that first peak almost all Mac users and I dare say ALL Mac developers have been looking forward to this release.  Unfortunately, today is also a day that Mac developers are going to be scrambling like rats on a burning ship.  This is because Apple decide to thumb their nose at us.
Further development of Bullfrog 2 for OS X
By on October 11, 2007 No Comments
Things are starting to speed up on the development of Bullfrog 2 previously mentioned here.  In our continuing disclosure about the development process, Jon Trainer has written a very informative article on how the actors are configured.
Starting over is hard to do
By on September 18, 2007 No Comments
I mentioned before that I am working with Jon Trainer of Outer Level to write Bullfrog 2 for OS X.  This development effort will be targeted at the as of yet unreleased OS X 10.5 Leopard.  We also promised to blog about the development as it moved forward.  Unfortunately not everything has been roses.
Cocoa® Programming for Mac® OS X, 2nd Edition

Like this article? We recommend
Cocoa® Programming for Mac® OS X, 2nd Edition

Transmitting an XML Document

Transmitting the XML document to a server is a matter of opening an NSURLConnection to the server and posting the document:

- (void)transmitXMLRequest:(NSData *)data
{
  NSURL *webServiceURL = [NSURL URLWithString:@"http://test.com/xmlResponder"];
  NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:webServiceURL];
  [urlRequest setHTTPMethod:@"POST"];
  [urlRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
  [urlRequest setHTTPBody:data];
  
  NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
  if (!connectionResponse) {
    NSLog(@"Failed to submit request");
  } else {
    NSLog(@"Request submitted");
    responseData = [[NSMutableData alloc] init];
  }
}

In the first line I constructed an NSURL object which points to the Web service I will be posting against. I then constructed an NSMutableURLRequest against that URL and set it up as an HTTP POST request. I then set the NSData representation of the XML document as the body of the request. Finally, I constructed an NSURLConnection with the NSMutableURLRequest. The construction of the NSURLConnection is what actually kicks off the post to the server.

Assuming that the NSURLConnection initializes properly, I then initialized the receiving NSMutableData object. As the data is returned from the server it will be stored in this object until the receiving portion of the flow is complete.

In the construction of the NSURLConnection I was required to pass in a delegate because NSURLConnection actually spins off a thread that will block and wait for a response from the server. As the response comes in from the server, the NSURLConnection calls one of several methods on the delegate. The methods are as follows:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

This method is normally called only once during the life of the NSURLConnection. It is normally called at the start of the receipt of data. However, if the server does any URL redirects, it is possible that this method will be called multiple times. Therefore, whenever this method is called, the data received from the server should be reset as follows:

  [responseData setLength:0];

This will ensure that the data stream is not corrupted by any URL redirects.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

This method will be called multiple times by the NSURLConnection. Whenever a block of data is received from the server, the data is passed to this method. The data being passed in should just be appended to the end of the NSMutableData object:

  [responseData appendData:data];

The exact side of each of these pieces is dependent on a great many factors that are outside the scope of this article. It is a safe assumption that this method will be called many times during the life of the NSURLConnection and therefore should be fairly efficient. It would not be wise to put any lengthy processing in this portion of the flow.

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

If at any time during the receipt of data there is an unrecoverable error, this method will be called. This method is a termination of the NSURLConnection. At this point, the NSURLConnection and the NSMutableData objects should be released and the error processed:

  NSLog(@"Error receiving response: %@", error);
  [connection release];
  [responseData release];

In this example I simply reported the error to the console and released the objects. In a production application it would be wise to report the error to the user.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

This method is called at the successful conclusion of the transmission. After this method is called, all the data has been received and the NSURLConnection can be safely closed.

  [connection release];
  [self deconstructXMLResponse:responseData];
  [responseData release];

In this example I simply released the NSURLConnection and passed the received data to the next step. After that was completed, I released the NSMutableData object.

The NSURLConnection object makes it very simple to talk to any HTTP Server on the Internet, and either retrieve data or post data to the server. Because the NSURLConnection handles all the messy details of threading for me, my application can remain responsive during the transmission without having to work with threads myself.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

User Group Organizations: Finding Support in the Greater IT Community
By Emily Nave on July 29, 2010 1 Comment

Birds of a feather flock together, right? If you’re already a member of an established user group or looking for other like-minded technology evangelists, connecting with peers is an important part of being an active voice in the IT community.

 Big Nerd RanchAsk Big Nerd Ranch: Blocks in Objective-C
By Big Nerd Ranch on June 24, 2010 No Comments

Adam Preble answers a question about blocks.

How Long?
By John Traenkenschuh on May 28, 2010 No Comments

John is in shock as the Apple Juggernaut rolls forward.  How long before Win Mobile gets the respect it deserves?

See All Related Blogs

Informit Network