以下代码在 iPad 应用程序中用于向 Node.js Web 服务器发送 HTTP 请求,这会产生以下错误,但使用常规 HTML+浏览器表单可以正常工作。
服务器是 Node.js +强大的,它有一个多部分解析器,它只在这行代码上死掉,并出现以下错误:
消息:解析器错误,已解析 29162 个字节中的 0 个
堆栈:错误:解析器错误,29162 个字节中的 0 个在 IncomingForm.write (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:120:17) 处解析传入消息。(/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:73:12)
at IncomingMessage.emit (events:27:15) at HTTPParser.onBody (http:100:23) at Stream.ondata (http:763:22) at IOWatcher.callback (net:494:29) at node.js:768:9
这是 iPad 代码:
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[theRequest setTimeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
//media
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"iosaudio.cai\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theAudio]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[theRequest setHTTPBody:body];
发送的请求是否格式错误?如果是这样,为什么以及应该如何做?