PDA

View Full Version : Problem with NSStream Sending the XML string to WS...!!!!!!




RashiMahajan
Aug 17, 2010, 02:18 PM
Hi,
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:@"abc.com"]];

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"abc.com", 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];

}
else
{
}

}


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];
}
}
}




break;
case NSStreamEventEndEncountered:
NSLog(@"I am here NSStreamEventEndEncountered");
break;
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)];
if(len>0)
{
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.......



RashiMahajan
Aug 18, 2010, 03:16 AM
ny one to help!!!!!!!