Advertisement
Guest User

Sasi

a guest
Sep 14th, 2010
5,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (IBAction)onConnect: (id)sender
  2. {
  3.     recordResults = NO;
  4.     NSString *soapMessage = [NSString stringWithFormat:
  5.                              @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  6.                              "<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"
  7.                             "<soap:Body>\n"
  8.                             "<GetHotItems xmlns=\"http://www.mysite.com/WSMobileData/\" />\n"
  9.                             "</soap:Body>\n"
  10.                             "</soap:Envelope>\n"
  11.                              ];
  12.    
  13.     NSLog( @"soapMessage: %@", soapMessage);
  14.    
  15.     // URL connection
  16.     NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/WSMobileData/WSMobile.asmx"];
  17.     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  18.     NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]
  19.    
  20.     [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  21.     [theRequest addValue: @"http://www.mysite.com/WSMobileData/GetHotItems" forHTTPHeaderField:@"SOAPAction"];
  22.     [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
  23.     [theRequest setHTTPMethod:@"POST"];
  24.     [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
  25.    
  26.     NSLog(@"theRequest: %@", theRequest);
  27.    
  28.     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  29.     if( theConnection )
  30.     {
  31.         webData = [[NSMutableData data] retain];
  32.     }
  33.     else
  34.     {
  35.         NSLog(@"theConnection is NULL");
  36.     }
  37. }
  38.  
  39. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  40. {
  41.     [webData setLength: 0];
  42. }
  43. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  44. {
  45.     [webData appendData:data];
  46. }
  47. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  48. {
  49.     NSLog(@"ERROR with theConenction");
  50.     [connection release];
  51.     [webData release];
  52. }
  53. -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  54. {
  55.     NSLog(@"DONE. Received Bytes: %d", [webData length]);
  56.     NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
  57.     NSLog(@"theXML: %@", theXML);
  58.     [theXML release];
  59.    
  60.     if( xmlParser )
  61.     {
  62.         [xmlParser release];
  63.     }
  64.    
  65.     xmlParser = [[[NSXMLParser alloc] initWithData: webData] autorelease];
  66.     [xmlParser setDelegate: self];
  67.     [xmlParser setShouldResolveExternalEntities: YES];
  68.     [xmlParser parse];
  69.    
  70.     [connection release];
  71.     [webData release];
  72. }
  73.  
  74. -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
  75.    attributes: (NSDictionary *)attributeDict
  76. {
  77.     if( [elementName isEqualToString:@"GetHotItemsResult"])
  78.     {
  79.         if(!soapResults)
  80.         {
  81.             soapResults = [[NSMutableString alloc] init];
  82.         }
  83.         recordResults = YES;
  84.     }
  85. }
  86. -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  87. {
  88.     if( recordResults )
  89.     {
  90.         [soapResults appendString: string];
  91.     }
  92. }
  93. -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  94. {
  95.     if( [elementName isEqualToString:@"GetHotItemsResult"])
  96.     {
  97.         recordResults = NO;
  98.         NSLog(@"soapResults: %@", soapResults);
  99.         [soapResults release];
  100.         soapResults = nil;
  101.     }
  102. }
  103.  
  104. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement