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
My need is sample code - that can explain me how to make connection & retrieve data from asp.net web service.
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.
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:)];
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.
- (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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With