开发者

Mac OS NSURLConnection in Iteration

I really need some help with an issue I am having with NSURLConnection in an iteration. I am building a command line app in Mac OS that sends a jpg to a server. Now every 30 seconds it checks for new images on the computer, if it finds one it sends it up to our server using HTTP Post (I looked into FTP but didn't find much support and HTTP appears to be faster for smaller files). So what is happening is that the POST will send the data correctly every other iteration but timeout on the rest. I have no idea why it would be doing this. I tried both Synchronous and Asynchronous request and they both have the same result. Here is the function that is being called to POST the images:

+ (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] retain];

     NSString *upload_url = [self getPlistValue:@"config.plist" withKey:@"upload_url"];
     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", 
                [self getPlistValue:@"config.plist" withKey:@"upload_protocol"],
                upload_url]];
     [request setURL:url];
     [request setTimeoutInterval:10];
       开发者_如何学JAVA [request setHTTPMethod:@"POST"];
     [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

        NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882%d",arc4random() % 74];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

     NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"client\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:[self getPlistValue:@"config.plist" withKey:@"client_dir"]] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"fname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithString:filename] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"jpg_img\"; filename=\"%@.%@\"\r\n",filename,@"jpg"]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [request setHTTPBody:body];

     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
     NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
     [body release];
     [request release];
     [returnData release];
     return ([returnString isEqualToString:@"DONE"]); 
}

What I find very mind boggling about this is that if I comment out the ImageData portion of the body every request completes successfully (so I know it has nothing to do with the receiving server). I also know the ImageData is a valid JPG because right before this function call I save it back to disk (for sanity check).

Does anyone have any ideas of why this would be happening? Any insight would be GREATLY appreciated.

UPDATE: I was able to get it working by implementing the ASIHTTPRequest Package, I guess I wasn't setting up the Multi-part form parameters correctly.


Check out this link: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

I can't pinpoint your exact problem, but the relevant code snippet from the above link that I think will help you is:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed@"localimage.png"]);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
        initWithURL:[NSURL 
        URLWithString:@"http://www.cimgf.com/testpostimage.php"]]; // Not a real URL. 

[request setHTTPMethod:@"POST"];
[request setValue:@"image/png" 
        forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%d", 
        [imageData length]] 
        forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:imageData];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜