(更新)我正在尝试读取一个大文件(视频或图片)并通过 SOAP 请求将其发送到远程服务器。我需要将数据编码为 Base64 字符串。我正在尝试按以下方式执行此操作:
为将“绕过”base64 编码数据的 SOAP 请求创建一个模板 xml
将 SOAP xml 的第一部分推入缓冲区
打开视频文件并将其编码为块并将每个编码的块推入缓冲区
最后,推送 SOAP xml 的第二部分
为了能够将上述部分“排队”,我正在尝试使用具有缓冲功能的 GCDAsyncSocket。我认为由于 GCDAsyncSocket 在 TCP 级别上运行,我需要自己编写 HTTP POST 标头。所以,有很多我只是模糊理解的活动部分,我可能做错了。但我的插座似乎从来没有起飞,我什至不知道如何调试它。这是我的相关代码,试试看你是否发现任何明显的错误:
NSString *soapBody = ...; //Create the SOAP xml here with the part in the middle reserved for the Base64 encoded data (marked with string "CUT_HERE";
NSArray *soapBodyArray = [soapBody componentsSeparatedByString:@"CUT_HERE"];
self.p_soapBodyPart1 = [soapBodyArray[0] dataUsingEncoding:NSUTF8StringEncoding];
self.p_soapBodyPart2 = [soapBodyArray[1] dataUsingEncoding:NSUTF8StringEncoding];
socketQueue = dispatch_queue_create("socketQueue", NULL);//socketQueue is an ivar
self.p_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:socketQueue];//create the socket
NSError *err = nil;
if (![p_socket connectToHost:myURL onPort:80 error:&err]) // Asynchronous!
{
NSLog(@"I goofed: %@", err);
return;
}
NSString* httpHeader = [NSString stringWithFormat:@"POST %@ HTTP/1.1\r\nHost: %@\r\nAccept-Encoding: gzip, deflate\r\nContent-Type: text/xml\r\nAccept-Language: en-us\r\nAccept: */*\r\nSOAPAction: http://tempuri.org/myAction\r\nConnection: keep-alive\r\nUser-Agent: ...\r\n\r\n", webserviceOperationsPath, webserviceHost];//Create the HTTP POST header
[p_socket writeData:[httpHeader dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1]; //enqueue the HTTP header
[p_socket writeData:self.p_soapBodyPart1 withTimeout:-1 tag:2]; //enqueue the first part of the SOAP xml
[self setUpStreamsForInputFile: [self.p_mediaURL path]];//set up NSInputStream to read from media file and encode it as Base64
套接字似乎总是可以正常连接,我看到使用这个委托方法:
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSLog(@"Socket Connected");
}
setUpStreamsForInputFile 方法(在上面的第一个代码清单中调用):
- (void)setUpStreamsForInputFile:(NSString *)inpath {
self.p_iStream = [[NSInputStream alloc] initWithFileAtPath:inpath];
[p_iStream setDelegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, ^ {
[p_iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[p_iStream open];
[[NSRunLoop currentRunLoop] run];
});
}
现在,前一个方法中的 NSInputStream 设置将向这个委托发送事件:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
switch(eventCode) {
case NSStreamEventHasBytesAvailable:
{
if (stream == self.p_iStream){
if(!self.p_tempMutableData) {
self.p_tempMutableData = [NSMutableData data];
}
uint8_t buf[24000];
unsigned int len = 0;
len = [p_iStream read:buf maxLength:24000];//read a chunk from the file
if(len) {
[p_tempMutableData appendBytes:(const void *)buf length:len];
NSString* base64encData = [Base64 encodeBase64WithData:self.p_tempMutableData];//encode the chunk
self.p_streamEncData = [base64encData dataUsingEncoding:NSUTF8StringEncoding];
[p_socket writeData:self.p_streamEncData withTimeout:-1 tag:3];//write the encoded chunk to the socket
}
}
break;
}
case NSStreamEventEndEncountered:
{
[stream close];
[stream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
stream = nil;
[p_socket writeData:self.p_soapBodyPart2 withTimeout:-1 tag:4];//write the second part of SOAP xml
break;
}
... //some other events handled here
}
}
套接字应该使用此委托将内容输出到日志
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
if (tag == 1)
NSLog(@"HTTP Header Written");
else if (tag == 2)
NSLog(@"Soap Part 1 written");
else if (tag == 3)
NSLog(@"File written");
else if (tag == 4)
NSLog(@"Soap Part 2 written");
}
但这是随机发生的。例如,有时我看到前 2 个 if 被调用,有时却没有。当我这样做并且它到达第三个“如果”(我正在写入实际编码数据的那个)时,它只写入了 2 或 3 次,仅此而已 - 考虑到文件的大小,我认为次数太少了。我从来没有看到它到达最后一个“if”,它应该写 SOAP xml 的最后一部分。将不胜感激任何帮助!提前致谢。
进一步更新 (3/19/13)
今天测试套接字时,我根本不再收到写入事件,这告诉我它是随机的,我做错了什么。今天连接打开,但在某个时候超时,正如我使用以下委托方法所看到的:
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{ // This method is executed on the socketQueue (not the main thread)
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
NSLog(@"socketDidDisconnect:withError: \"%@\"", err);
}
});
}
返回
socketDidDisconnect:withError: "错误域=NSPOSIXErrorDomain Code=60 "操作超时" UserInfo=0x1cd89b00 {NSLocalizedFailureReason=connect() 函数出错,NSLocalizedDescription=操作超时}"
虽然我仍在上面的流委托中运行 Base64 数据的写入。