NSURLConnection didReceiveResponse not recognizing multiple MIME types
I am working on a webcam viewer, for these cams: http://www.canvision.net/support/pt300-cgi/GetData.htm
the data arrives like this:
HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: image/jpeg
These are my NSURLConnection Delegate methods:
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
{
[currentData appendData:data];
}
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"response with mime: %@ length:%i",[response MIMEType],[currentData length]); if([[response MIMEType] isEqualToString:@"image/jpeg"] && [currentData length] != 0){ currentFrame = [UIImage imageWithData:currentData]; NSLog(@"valid frame"); [image setImage:currentFrame]; frameCounter++; } else { NSLog(@"other data:%@",currentData); } currentFrame = nil; if (currentData == receivedData1)//swap buffers currentData = receivedData2; else currentData = receivedData1; [currentData setLength:0]; [currentFrame release]; } This all works fine, and the console output looks as you would expect:
[Session started at 2011-02-21 20:20:39 +0100.]
2011-02-21 20:20:43.237 MotionJPEG[16330:207] response with mime: multipart/x-mixed-replace length:0
2011-02-21 20:20:43.261 MotionJPEG[16330:207] ot开发者_JS百科her data:<>
2011-02-21 20:20:43.262 MotionJPEG[16330:207] response with mime: image/jpeg length:0
2011-02-21 20:20:43.263 MotionJPEG[16330:207] other data:<>
2011-02-21 20:20:43.340 MotionJPEG[16330:207] response with mime: image/jpeg length:4608
2011-02-21 20:20:43.340 MotionJPEG[16330:207] valid frame
2011-02-21 20:20:43.445 MotionJPEG[16330:207] response with mime: image/jpeg length:4600
2011-02-21 20:20:43.446 MotionJPEG[16330:207] valid frame
2011-02-21 20:20:43.544 MotionJPEG[16330:207] response with mime: image/jpeg length:4580
2011-02-21 20:20:43.545 MotionJPEG[16330:207] valid frame
However, the Cam supports another mode, where additional infos are embedded between the jpegs:
HTTP/1.0 200 OK
Date: Wed, 19 Feb 2003 03:40:16 GMT
Server: WYM/1.0
Connection: close
Content-Type: multipart/x-mixed-replace;boundary=WINBONDBOUDARY
Last-Modified: Wed, 19 Feb 2003 03:40:16 GMT
Pragma: no-cache
Cache-Control: no-cache
Expires: 01 Jan 1970 00:00:00 GMT
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: text/plain
--WINBONDBOUDARY
Content-Type: image/jpeg
--WINBONDBOUDARY
Content-Type: text/plain
In this mode I would expect the code to work ok, and the text/plain part to be written to the console by the
NSLog(@"other data:%@",currentData);
line. however, for some reason, the output looks exactly the same, and it never says response with mime: text/plain instead, the plaintext part is appended to the jpeg, and UIImageView is unable to display the data. Why does NSURLConnection not recognize the text/plain part? Here is the complete source: http://renehopf.de/MotionJPEG.zip but to reproduce the problem you would need the same webcam...
Thanks,
Rene
精彩评论