user2273497
user2273497

Reputation: 11

Check if url exists without checking HTTP response

I have a scenario in which when HTTP status code is not equal to 200 i.e HTTP_OK , then I have to make some modifications accordingly.But I do not want to do these changes inside - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response which is the delegate method of NSURLConnectionDelegate.

Is there any other way by which I can find out that URL will not open successfully without checking the response status codes?

Upvotes: 1

Views: 795

Answers (5)

Sulthan
Sulthan

Reputation: 130132

A resource identified by a URL with a HTTP scheme (http:) can be only accessed through a HTTP service. That HTTP service has the power to decide if the resource exists or not depending on HTTP method or HTTP headers (e.g. Authorization).

In other words, a HTTP resource cannot be accessed by any other means than through HTTP.

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53870

I feel the same way. I handle the response code in didFinishLoading or didFailWithError.

I just create a property named lastHTTPResponse of type NSHTTPURLResponse
and set it in didReceiveResponse, then handle it later. Set it with each response, so you get the last code and not any redirects (301 or 302).

If you have more than one connection, you will want a property for each.

Sometimes, you want to handle the response early, but when you are calling your own server, most of the time, it's not necessary.

Upvotes: 0

M.Sameer
M.Sameer

Reputation: 3151

You have to check the response code as you do now but instead of doing HTTP GET or POST request you can make HTTP HEAD request to return the headers only without the content. I used to do this to check for broken links retrieved from web in an Android App.

Upvotes: 1

Toam
Toam

Reputation: 362

I don't think there is another way. If you don't want to load all the response, you can kill the request after you received the HTTP code with [connection cancel].

You can also take a look at didFailWithError but it does not work for 404 and 500 errors...

Upvotes: 0

Kevin Dettki
Kevin Dettki

Reputation: 66

IMO there´s no way to find the information you´re searching for without checking the response.

If you´re trying to keep the check in the actual running main Thread you can use sendsynchronousrequest to get the response within your actual code block.

But keep in mind, that this will block your main Thread until an Response is returned.

Upvotes: 0

Related Questions