Re: accessing the CFSteam that NSURLConnection is using?
Re: accessing the CFSteam that NSURLConnection is using?
- Subject: Re: accessing the CFSteam that NSURLConnection is using?
- From: Becky Willrich <email@hidden>
- Date: Wed, 12 Oct 2005 15:30:23 -0700
On Oct 12, 2005, at 2:04 PM, sean wrote:
Excellent, this is a great jump start from where I was stuck, thank
you.
I converted my NSURLRequest to use a NSInputStream instead of just an
NSData, which seems to be working great. Sample code below:
// [snip]
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString
stringWithFormat:@"--%@\r\n",stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString
stringWithFormat:@"Content-Disposition: form-data; name=\"upload\";
filename=\"%@\"\r\n", [imagePath lastPathComponent]]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type:
application/octet-stream\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithContentsOfFile:imagePath]];
[postBody appendData:[[NSString
stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// [postRequest setHTTPBody:postBody];
[postRequest setHTTPBodyStream:[NSInputStream
inputStreamWithData:postBody]];
// [snip]
I have the logic twisted around in my head at the moment, and I'm not
sure how to determine how much data has been written to the server via
the POST request from NSInputStream (conceptually, it seems like this
should be an NSOutputStream, doesn't it?)
Meanwhile, back at Bat headquarters (after the POST connection is
connected), this isn't working:
NSInputStream *requestInputStream = [eachUrlRequest HTTPBodyStream];
unsigned int * numBytes;
uint8_t * buffer;
if ( [requestInputStream getBuffer:&buffer length: numBytes] ) {
NSLog(@"Sweet, we got the numBytes! length: %d", numBytes);
} else {
NSLog(@"Nimrod!, you did it wrong again!");
}
The problem with this approach is that the NSURLConnection, not you,
needs to open and read from the body stream. You are simply
producing the body stream for its use. Here's what you need:
- create a stream for the post body. Looks like you have that code.
- create an NSInputStream subclass (let's call it PostingStream) that
initializes from another NSInputStream and passes all messages from
itself to the other stream. Override the methods read:maxLength: and
getBuffer:length: so that they perform the read on the other stream,
then somehow notify your code of how many bytes were read.
- create a PostingStream from the stream for the post body
- set the PostingStream as the HTTPBodyStream on the outgoing request
- hook up whatever notification mechanism you've created so you'll
get notified as bytes are transmitted
- Now create the NSURLConnection as normal, sit back, and wait for
your notifications to come in
Hope that helps,
REW
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden