Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mycompuser

macrumors member
Original poster
May 8, 2012
37
0
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/uploading-files-from-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?.
 

ramy1989

macrumors newbie
Nov 7, 2012
21
0
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.
 

mycompuser

macrumors member
Original poster
May 8, 2012
37
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,748
8,421
A sea of green
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.
 

mycompuser

macrumors member
Original poster
May 8, 2012
37
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.