Solved - sorta (Was: NSURLConnection & NSInputStream issue)
Solved - sorta (Was: NSURLConnection & NSInputStream issue)
- Subject: Solved - sorta (Was: NSURLConnection & NSInputStream issue)
- From: David LeBer <email@hidden>
- Date: Tue, 16 Dec 2008 08:06:14 -0500
On 15-Dec-08, at 4:45 PM, David LeBer wrote:
Hello all,
I have a requirement where I need to send a chunked file (about 2MB
at a time) to a server. Each chunk is embedded as a base64 encoded
string in the body of an HTML POST request.
I'm working on a proof of concept dev spike and I've got this code
which fails:
- (void) callUploadAssetChunk
{
NSString *contentString = [[NSString alloc]
initWithFormat:@"contactInfoXML=%@&mediaValueID=%@&hash=
%@&chunkIndex=%d&chunk=%@",
[self contactInfoXML],
[[self currentAsset] mediaValueId],
[[[self currentAsset]
currentUploadChunk] chunkHash],
[[[self currentAsset]
currentUploadChunk] chunkIndex],
[[[self currentAsset]
currentUploadChunk] encodedChunkData]];
NSData *contentData = [ NSData dataWithBytes: [ contentString
UTF8String ] length: [ contentString length ] ];
NSString *path = [NSString stringWithFormat:@"%@/
UploadAssetChunk", kWebMethodsFull];
NSURL *url = [[NSURL alloc] initWithScheme:kURLScheme host:
[uploadUrl stringValue] path:path];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:url];
[request setHTTPMethod: @"POST"];
NSInputStream *dataStream = [NSInputStream
inputStreamWithData:contentData];
[request setHTTPBodyStream:dataStream];
[self setChunkUploadConnection:[NSURLConnection
connectionWithRequest:request delegate:self]];
[url release];
[request release];
}
In looking at the wire, I see the HTTP headers go by, but there is
no body content. If I bypass the NSInputStream with the NSData and
replace:
[request setHTTPBodyStream:dataStream];
With:
[request setHTTPBody:contentData];
Everything works fine.
I am testing with an NSInputStream because I'd like to be able to
query it to get an upload progress for the user. So, it looks like I
am either misreading the NSURL* API (possible) or missing something
obvious in creating the NSInputStream (also possible). If anyone has
any suggestions, I'd love to hear it.
It is so frustratingly close. If it weren't for those darn needy
users and their dislike of not knowing what was going on, I'd be
done now :-)
I still don't know why this wasn't working but after mucho reading I
came to the conclusion that even if it did, there was no guarantee I'd
be able to get the progress info I needed from the NSInputStream so I
decided to punt. I ended up using Dave Dribin's very nice DDCurl*
libcurl wrapper.
It already has DDCurlConnection and DDMutableCurlRequest which are
very similar in API to the NSURL*, I just had to modify
DDMutableCurlRequest to add the HTTPBody attribute and I was off to
the races.
Progress indicator progressing and everything.
*< http://www.dribin.org/dave/software/>
;david
--
David LeBer
Codeferous Software
'co-def-er-ous' adj. Literally 'code-bearing'
site: http://codeferous.com
blog: http://davidleber.net
profile: http://www.linkedin.com/in/davidleber
twitter: http://twitter.com/rebeld
--
Toronto Area Cocoa / WebObjects developers group:
http://tacow.org
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden