Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 22, 2012, 11:03 AM   #1
mycompuser
macrumors newbie
 
Join Date: May 2012
Uploading file using NSURLConnection

Dear all,

In my app developed in Lion using XCode, I need to determine the speed of the internet connection (upload speed) and for that I upload an file using NSURLConnection.
I extracted the code for formatting the xml and uploading from
http://blog.grio.com/2009/09/uploadi...om-iphone.html

I am using the async version of NSURLConnection to free up the main UI and also as I want the progress of the uploading process.

I have implemented the delegate methods provided by NSURLConnection they are.
Code:
//1. Method fired after upload is complete along with response code.
- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response

//2. Method fired when upload process fails.
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error

//3. Method fired when upload process is completed.
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection

//4. Fired periodically indicating no. of bytes sent/uploaded to the server (can be used to determine upload speed).
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
I find that even if I configure an invalid Upload URL for uploading of an file, the delegate method 4. above gets fired periodically during the uploading process giving the impression that the uploading is being performed successfully.

However during the end of the upload process with an invalid upload URL(when totalBytesWritten == totalBytesExpectedToWrite), the method 1. fires with an error http 404 indicating that the upload process has failed.

Is there a mechanism by which I can find whether the uploading of is being carried out successfully during the actual uploading of the file rather than waiting till the entire file is uploaded?.
mycompuser is offline   0 Reply With Quote
Old Nov 25, 2012, 05:19 AM   #2
ramy1989
macrumors newbie
 
Join Date: Nov 2012
When you send a HTTP request, even if the request is invalid you get a response.In the response message there is written the error code.In this case the error is 404 so probably the URL is wrong.Hope it helps.
ramy1989 is offline   0 Reply With Quote
Old Nov 25, 2012, 09:17 AM   #3
mycompuser
Thread Starter
macrumors newbie
 
Join Date: May 2012
Yes. You are right.

But i find that the http response arrives only after the entire file is uploaded (even though the upload was never successful).
I don't get any http response stating an error for the current upload (as the url is invalid) during the initiation of the upload or during the upload process itself.

Only after the entire file is uploaded (after which i receive the http resone) can I determine if the file upload was successful or not.

Is there an mechanism by which I can figure if the upload url is invalid or not without having to upload an entire file?
mycompuser is offline   0 Reply With Quote
Old Nov 25, 2012, 01:14 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by mycompuser View Post
But i find that the http response arrives only after the entire file is uploaded (even though the upload was never successful).
I don't get any http response stating an error for the current upload (as the url is invalid) during the initiation of the upload or during the upload process itself.

Only after the entire file is uploaded (after which i receive the http resone) can I determine if the file upload was successful or not.
Yes, because that's how HTTP works. The client (your program) makes a request, which must be a complete request. The server then responds. The client (i.e. the one making the request) normally doesn't engage in a back-and-forth conversation. Doing so would be stateful, whereas HTTP is designed as a stateless protocol.

If you don't know how the protocol actually works, you should probably study that. It's not a complex protocol in its typical use. Only the 1xx responses really have any degree of complexity for handling them.


As a strategy for using HTTP, you could send a small POST or PUT request first, and see if it works. If it works, only then do you send a larger POST or PUT. If the small request fails, you can reasonably predict the larger request will also fail.

In any case, this assumes you have some control over what the HTTP server is doing. If you have no control over the server at all, then you have no way to control whether it even accepts a POST or PUT request, much less how it responds to them.

It's unclear what the overall context of this upload is, and what kind of error handling (e.g. failure responses from server) your program actually needs to perform.

Any server will have constraints, and if your program doesn't work within them, your uploads will fail. So if you don't have a strategy for working within the constraints, by designing your program to handle those constraints, then you will eventually encounter an error you can't handle, and your program will fail. Even a public web-service like AWS S3 has constraints on uploads. If you don't know what your server's constraints are, then you have more research to do.
chown33 is offline   0 Reply With Quote
Old Nov 27, 2012, 07:10 AM   #5
mycompuser
Thread Starter
macrumors newbie
 
Join Date: May 2012
Thanks for the reply.

Yes. The alternate approach that I had in mind to resolve this issue was for me to upload an small file (few kb's) first and check for the http response. This will indicate whether the url is valid or not.

Though this approach has a little overhead in the form of additional time it takes, looks like i don't have any alternatives either.
mycompuser is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 10:58 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC