Problem with NSStream Sending the XML string to WS...!!!!!!

Discussion in 'iOS Programming' started by RashiMahajan, Aug 17, 2010.

  1. RashiMahajan macrumors newbie

    Apr 17, 2009
    I am trying to post my XML string to the server by using stream, Posting my code as follow:

    - (void)_startSend:(NSString *)message
    	NSString *data = message;
    	[self setOutData:message];
    	NSData *streamData = [data dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];
    	CFReadStreamRef readStream;
    	CFWriteStreamRef writeStream;
    	CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"", 80, &readStream, &writeStream);
    	self.outStream = [[NSOutputStream alloc]initToMemory];
    	self.inStream = (NSInputStream *)readStream;
    	self.outStream = (NSOutputStream *)writeStream;
    	[self.inStream retain];
    	[self.outStream retain];
    	[self.inStream setDelegate:self];
    	[self.outStream setDelegate:self];
    	[self.inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    	[self.outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    	[self.inStream open];
    	[self.outStream open];
        self.inStream =[NSInputStream inputStreamWithData:streamData];
    	NSUInteger fileLength = [streamData length];
    	[request setHTTPMethod:@"POST"];
    	//[request setHTTPBodyStream:self.inStream];
    	[request setTimeoutInterval:200];
    	[request setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    	[request setValue:[NSString stringWithFormat:@"%i", fileLength] forHTTPHeaderField:@"Content-Length"];
    	[request setValue: @"xmyx" forHTTPHeaderField:@"SOAPAction"];
    	NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self]retain];
    	if( theConnection )
    		webData = [[NSMutableData data] retain];
    In Delegates :

    - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)streamEvent
    	NSLog(@"I am in stream delegate & stream is %@ wd event is %i",aStream, streamEvent);
    	switch(streamEvent) {
            case NSStreamEventHasBytesAvailable:
    			NSLog(@"I am here  NSStreamEventHasBytesAvailable");
    		   uint8_t buffer[1024];  
                    int len;  
    		if([self.inStream hasBytesAvailable])
    			NSLog(@"The stream is reading");
                        len = [self.inStream read:buffer maxLength:sizeof(buffer)];  
                        if (len > 0)  
                            NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];  
    						NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
    						if (nil != output)  
                              NSArray * bufferArray = [[NSArray alloc] init];  
                                bufferArray = [output componentsSeparatedByString:@"\n"];  
                                NSLog(@" OutPUt is %@", output);  
                                [output release];  
            case NSStreamEventEndEncountered:
    			NSLog(@"I am here  NSStreamEventEndEncountered");
            case NSStreamEventHasSpaceAvailable:
    			NSLog(@"I am here  NSStreamEventHasSpaceAvailable");
    			NSData *streamData = [outData dataUsingEncoding:NSUTF8StringEncoding];
    			uint8_t* buf=(uint8_t*)[outData UTF8String];
    			[self.outStream write:buf maxLength:strlen((char*)buf)];  
    				NSLog(@"command send");
    				[self.outStream close];

    i get the call for NSStreamEventHasSpaceAvailable in delegate function while hitting the Web services but i receive 0 bytes in response. but if i uncomment the line //[request setHTTPBodyStream:self.inStream]; i get error from the server that root elemnt cant be null blahhh blahhh .......... i have never worked on the streaming stuff before so this is quite annoying ... Could any one let me know/ share code snippets for posting my XML string to the server by using streaming.... I will be very thankful to you..........

    Waiting for loads of replies buddies.......
  2. RashiMahajan thread starter macrumors newbie

    Apr 17, 2009

Share This Page