Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 468
Sagar Kothari Avatar asked Aug 17 '09 22:08

Sagar Kothari


4 Answers

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.

like image 56
Daniel Avatar answered Oct 09 '22 22:10

Daniel


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:)];
like image 36
PeyloW Avatar answered Oct 09 '22 21:10

PeyloW


If you're not integrating with ASP.NET on both sides, I'd probably avoid a 'web service' specifically, and just output your own XML format, and process it appropriately on the iPhone side with an XML lib.

like image 1
Noon Silk Avatar answered Oct 09 '22 22:10

Noon Silk


- (void)viewDidLoad {
[super viewDidLoad];

// create a soap Message which is given in your required web service

NSString *soapMessage=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<GetCategory xmlns=\"http://tempuri.org/\" />\n"
"</soap:Body>\n"
"</soap:Envelope>";

// create a url to your asp.net web service.
NSURL *tmpURl=[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.32.10/Itavema/Admin/Itavema_Service.asmx"]];

// create a request to your asp.net web service.
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:tmpURl];

// add http content type - to your request
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

// add  SOAPAction - webMethod that is going to be called
[theRequest addValue:@"http://tempuri.org/GetCategory" forHTTPHeaderField:@"SOAPAction"];

// count your soap message lenght - which is required to be added in your request
NSString *msgLength=[NSString stringWithFormat:@"%i",[soapMessage length]];
// add content length 
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];

// set method - post
[theRequest setHTTPMethod:@"POST"];

// set http request - body
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

// establish connection with your request & here delegate is self, so you need to implement connection's methods
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

// if connection is established
if(con)
{
    myWebData=[[NSMutableData data] retain];
    // here -> NSMutableData *myWebData; -> declared in .h file
}
}   

// a method when connection receives response from asp.net web server
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[myWebData setLength: 0];
}
// when web-service sends data to iPhone
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myWebData appendData:data];
}
// when there is some error with web service
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
}
// when connection successfully finishes 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// check out your web-service retrieved data on log screen
NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
[theXML release];

// if parser isn't nil. here NSXMLParser *myXMLParser; in .h file
if( myXMLParser )
{
    [myXMLParser release];
}

// supply your responded data to xmlParser - xmlParser will parse xmlData & then you can use it
myXMLParser = [[NSXMLParser alloc] initWithData: myWebData];

// here delegate self means implement xmlParse methods
[myXMLParser setDelegate: self];

[myXMLParser setShouldResolveExternalEntities: YES];

// parse method - will invoke xmlParserMethods
[myXMLParser parse];
[connection release];
[myWebData release];
}


//#pragma mark xmlParser
// suppose <myDataTag>myData</endmyDataTag> is the xmlData
// this function will read "<myDataTag>" & tag attributes
-(void)parser:(NSXMLParser*)parser 
                            didStartElement:(NSString*)elementName
                            namespaceURI:(NSString*)namespaceURI
                            qualifiedName:(NSString*)qualifiedName
                            attributes:(NSDictionary*)attributeDict
{
    if([elementName isEqualToString:@"GetCategoryResult"])
    {
        // here categoryArray is NSMutable array declared in .h file.
        // init your array when root element / document element is found
        CategoryArray=[[NSMutableArray alloc]init];
    }
    else if([elementName isEqualToString:@"Prop_Category"])
    {
        aCategory=[[Category alloc] init];
        // if a tag has attribues like <myDataTag id="sagar">
        //aCategory.ID=[attributeDict objectForKey:@"id"];
    }
}

// suppose <myDataTag>myData</endmyDataTag> is the xmlData
// this function will read "myData" & tag attributes
-(void)parser:(NSXMLParser*)parser
                            foundCharacters:(NSString*)string
{
    // here currentElementValue is an NSMutableString declared in .h file
    // store read characters in that mutable string & then add to your object.
    if(!currentElementValue)
    {
        currentElementValue=[[NSMutableString alloc] initWithString:string];
    }
    else
    {
        [currentElementValue appendString:string];
    }
}

// suppose <myDataTag>myData</endmyDataTag> is the xmlData
// this function will read "</endmyDataTag>" & tag attributes
-(void)parser:(NSXMLParser*)parser
            didEndElement:(NSString*)elementName
            namespaceURI:(NSString*)namespaceURI
            qualifiedName:(NSString*)qualifiedName
{
    if([elementName isEqualToString:@"GetCategoryResult"])
    {
        // if end of root element is found- i.e. end of your xml file.
        return;
    }
    else if([elementName isEqualToString:@"Prop_Category"])
    {
        // end of a single data element
        // suppose <category>
        //             <id>10</id>
        //             <name><sagar></name>
        //         </category>
        // when we found </category> we have finished entire category object.
        // here we have an object aCategory -> created custom class "Category"
        // CategoryClass -> NSString *name; NSInteger id;
        // Note: "important"
        //->class variables must be same as tag

        // now after reading entire object add to your mutable array
        // now this mutable array can be used for Table, UIPicker
        [CategoryArray addObject:aCategory];
        [aCategory release];
        aCategory=nil;
        [CategoryTable reloadData];
    }
    else
    {
        // which is equivalent to aCategory.id=10 & aCategory.name=@"sagar"
        [aCategory setValue:currentElementValue forKey:elementName];

        // remove previously read data
        [currentElementValue release];
        currentElementValue=nil;
    }
}
like image 3
Sagar Kothari Avatar answered Oct 09 '22 20:10

Sagar Kothari