iPhone interaction with ASP.NET WebService

I have to develop an application, which will request to a web service developed in ASP.NET.

I don't know, what is the code of creating a request to asp.net web service,

How asp.net web service will respond to iPhone Application?

How iPhone will parse that response to proper way?

I have already read this question


How to Fetch Data From a WebService in iPhone?
But given link only gives the .pdf file.

My need is sample code - that can explain me how to make connection & retrieve data from asp.net web service.


Solution 1:

You can actually make a web service and make it real easy to integrate into your iphone up. I would suggest if you are using .net to create a WCF service with webHttp bidding and implement get and post methods, you can get responses back in json and xml (theres a set of classes to parse Json on the iphone that will make parsing the response a breeze, they are avaialble in the web), with little setup you will be able to perform gets and post from the iphone using NSURLRequest. Heres an article that talks about making a restful wcf service http://www.developer.com/net/article.php/10916_3695436_2. Its also very easy to add authentication and security to your services with WCF.

Solution 2:

Hessian is much better communication protocol than XML. Being a binary format it is even more compact, and with a strict format parsing is much faster.

As a bonus there are already frameworks for Java, .NET and PHP to expose a web service. Truly easy. Asume you have this C# interface:

public interface ITest {
  public string getGreeting();
  int addNumbers(int a, int b);
}

Then implementing it on the server using HessianC# is a snap:

public class CTest:CHessianHandler, ITest {
  public string getGreeting() { return "Hello World!"; }
  public int addNumbers(int a, int b) { return a + b; }
  [STAThread]
  private static void Main(string[] args) {
    CWebServer web = new CWebServer(5667, "/test/test.hessian", typeof (CTest));
    web.Paranoid = true;
    web.AcceptClient("[\\d\\s]");
    web.Run();
    for (;; ) {
      if (Console.ReadLine() != "") {
        web.Stop();
        break;
      }
    }
  }
}

On the iPhone side the C# interface need to be translated into an Objective-C protocol:

@protocol ITest
-(NSString*)getGreeting;
-(int)addNumbers:(int)a :(int)b;
@end

And then using HessianKit for getting a proxy for the service is almost as easy:

id<ITest> proxy = [CWHessianConnection proxyWithURL:serviceURL
                                           protocol:@protocol(ITest)];
NSLog(@"Greeting: %@", [proxy getGreeting]);
NSLog(@"The answer: %d", [proxy addNumbers:40 :2]);

In this short answer the method names are not quite C#-ish, an not quite Obj-C-ish either. This is because by default HessianKit uses Java's naming conventions. This can be overriden in HessianKit by providing method, and type name translations. So that both the C# and the Obj-C sides on the connection feels 100% at home. For example:

[CWHessianArchiver setClassName:@"com.mycompany.ITest" 
                    forProtocol:@protocol(CWTest)];
[CWHessianArchiver setMethodName:@"AddNumbers"
                     forSelector:@selector(addInt:toInt:)];