下面的代碼用於向iPad應用程序發送HTTP請求到Node.js Web服務器,該服務器會產生以下錯誤,但工作正常W /常規HTML +瀏覽器窗體。這是來自iPad的惡意格式的http請求,它會殺死Node.js多分析器
該服務器的Node.js + formidable具有多解析器僅this line of code死亡與此error:
message: parser error, 0 of 29162 bytes parsed
stack: Error: parser error, 0 of 29162 bytes parsed at IncomingForm.write (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:120:17) at IncomingMessage. (/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];
被請求發送格式不正確?如果是這樣,爲什麼以及如何做?
我不知道您是否生成格式錯誤的請求,但ASIHTTPRequest(http://allseeing-i.com/ASIHTTPRequest/)使創建多部分表單帖子變得簡單,您不再需要冒汗這些細節。 – 2010-10-14 04:56:49
在NSString文字('@「...」')上不需要使用'stringWithString:'。它已經是一個字符串了。我非常懷疑你也使用了'dataWithData:':你真的需要複製你已經擁有的數據對象嗎? – 2010-10-14 07:44:51
@Robot K - 謝謝你的訣竅 – roder 2010-10-16 03:51:11