NSInputStream不打开流/如何在设置流时发布数据

时间:2013-02-16 22:22:37

标签: objective-c streaming nsstream

我正在尝试做什么:我有一个网址请求(帖子),我将一些信息发送到api服务器,然后开始以字节为单位向我发送数据流。

1)如何在尝试设置流时发布数据现在我只是使用网址,我可以合并一些NSURLRequest吗?

2)为什么我的流甚至不打开(streamStatus返回0),因此永远不会调用- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode? ,这是我最好的尝试,大部分都是在Guide

之后
- (void)setUpStreamFromURL:(NSURL *)path {

     // iStream is NSInputStream instance variable
     iStream = [[NSInputStream alloc] initWithURL:path];
     [iStream setDelegate:self];
     [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                   forMode:NSDefaultRunLoopMode];
     [iStream open];

     NSLog(@"Stream Open: %lu",[iStream streamStatus]); //return 0
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
     NSLog(@"Streaming");
     switch(eventCode) {
         case NSStreamEventHasBytesAvailable:
         {
             if(!_data) {
                  _data = [[NSMutableData data] init];
             }
              uint8_t buf[1024];
              unsigned int len = 0;
              len = [(NSInputStream *)stream read:buf maxLength:1024];
              if(len) {
              [_data appendBytes:(const void *)buf length:len];
              NSLog(@"DATA BEING SENT : %@", _data);
              // bytesRead is an instance variable of type NSNumber.
             // [bytesRead setIntValue:[bytesRead intValue]+len]; //getting error that setInt value is not part of NSNumber, and thats true so not sure what to do about it, but this isn't the issue. 
              } else {
                    NSLog(@"no buffer!");
              } 
              break;
         }
         case NSStreamEventEndEncountered:
         {
              [stream close];
              [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                          forMode:NSDefaultRunLoopMode];
              stream = nil; // stream is ivar, so reinit it
              break;
         }
        // continued ...
     }
}

也有帮助,我的头文件:

 #import <Foundation/Foundation.h>
 #import "Login.h"

 @interface Stream : NSStream <NSStreamDelegate> {
NSMutableArray *searchIdList;
NSInputStream *iStream;
NSNumber *bytesRead;

}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode ;
-(id)initWithLoginObject:(Login *)log;
@property NSMutableData *data;
@end

1 个答案:

答案 0 :(得分:0)

您无法在Stream中使用NSURLRequest

要创建请求,您可以使用此方法。

request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"),(CFURLRef) url, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("User-Agent"), CFSTR("MSControl-US"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Type"), CFSTR("application/json"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Keep-Alive"));

之后,您可以使用

创建流
 readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request)

或者如果您已经打开了流,则可以使用以下命令序列化您的请求:

NSData *data = (NSData *)CFHTTPMessageCopySerializedMessage(request);

并发送此信息:

length = [data length];
CFWriteStreamWrite((CFWriteStreamRef)outStream,data,lenght);

希望这个帮助