我正在使用NSMutableURLRequest将图像上传到Web服务器。我认为我的代码是稳定的,因为它适用于大多数用户,但有些用户根本无法上传任何图像。看着我的服务器日志,我看到偶尔NSMutableURLRequest偶尔出现“无边界”错误
“中的multipart/form-data发布的数据缺少边界在未知在线0”警告这将指向一个边界错误,构建后的身体。
下面是我使用的是什么:
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[method uppercaseString]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data, boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
// add the image
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"image_upload\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *jpegData = UIImageJPEGRepresentation(jpeg, 100);
[postBody appendData:jpegData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add the other POST params
keys = [postdata allKeys];
int l = [keys count];
for (int i=0; i<l; i++){
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", [keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[postdata objectForKey:[keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
我可以理解,如果代码导致了当时的PHP警告100%,但它可能每30个或40个用户发生一次。如果用户得到这个错误,那么对于任何他们试图发布的图片来说,它都是一样的。
任何人都可以立即看到明显的东西,或有任何见解,为什么这将是一个间歇性的问题?