NSURLConnection and 404

  • This works fine, but blocks my for 1 sec on a good day - who know how long if the net is slow:

    url = something/which/might/exist/or/not.gif
    data = [ NSData dataWithContentsOfURL: url options: mask error: &outError ];

    So I am trying to use NSURLConnection instead. Also works fine.

    Only: if my url does not exist, then the old method returned data = nil. Very good.

    Using  NSURLConnection I always get some answer - either image data or a lengthy string like:

    HTML 4.0 Transitional//EN">
    <html>
    <head>
      <title>Error 404 - Page not found</title>
    </head>
    <body bgcolor="#000000">
    [...]

    Is there a way to get just the status (in this case 404) without parsing the data?
    I looked at the NSURLConnection delegate methods, but did not find anything which looked appropriate.

    Gerriet.
  • On Jun 9, 2013, at 5:35 AM, Gerriet M. Denkmann <gerriet...> wrote:

    > Is there a way to get just the status (in this case 404) without parsing the data?
    > I looked at the NSURLConnection delegate methods, but did not find anything which looked appropriate.

    I do something like this:

    - (void) connectionDidFinishLoading:(NSURLConnection *)connection
    {
            NSString *xmlString = [[NSString alloc] initWithData: receivedData encoding: NSUTF8StringEncoding];
            NSError *error = nil;

            if ([xmlString containsSubString: @"DOCTYPE"]) // the server gave an error in html format
            {
                error = [self serverErrorFromHTMLString: xmlString];
            }
            else
            {
              // no 404, etc status, so we can parse
                NSXMLParser *parser = [[NSXMLParser alloc] initWithData: receivedData];
    ....
    }

    // show alert with error if needed
    }

    - Koen.
  • Le 9 juin 2013 à 11:35, Gerriet M. Denkmann <gerriet...> a écrit :

    > This works fine, but blocks my for 1 sec on a good day - who know how long if the net is slow:
    >
    > url = something/which/might/exist/or/not.gif
    > data = [ NSData dataWithContentsOfURL: url options: mask error: &outError ];
    >
    > So I am trying to use NSURLConnection instead. Also works fine.
    >
    > Only: if my url does not exist, then the old method returned data = nil. Very good.
    >
    > Using  NSURLConnection I always get some answer - either image data or a lengthy string like:
    >
    > HTML 4.0 Transitional//EN">
    > <html>
    > <head>
    > <title>Error 404 - Page not found</title>
    > </head>
    > <body bgcolor="#000000">
    > [...]
    >
    >
    > Is there a way to get just the status (in this case 404) without parsing the data?
    > I looked at the NSURLConnection delegate methods, but did not find anything which looked appropriate.
    >
    > Gerriet.

    The NSConnectionDelegate protocol has a method that pass you the response:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    You can test if the response is of type NSHTTPURLResponse (which should always be the case when you send an HTTP request), and then use the -[NSHTTPURLResponse statusCode] method to get the status.

    -- Jean-Daniel
  • On 9 Jun 2013, at 16:54, Jean-Daniel Dupas <devlists...> wrote:

    >
    > Le 9 juin 2013 à 11:35, Gerriet M. Denkmann <gerriet...> a écrit :
    >
    >> This works fine, but blocks my for 1 sec on a good day - who know how long if the net is slow:
    >>
    >> url = something/which/might/exist/or/not.gif
    >> data = [ NSData dataWithContentsOfURL: url options: mask error: &outError ];
    >>
    >> So I am trying to use NSURLConnection instead. Also works fine.
    >>
    >> Only: if my url does not exist, then the old method returned data = nil. Very good.
    >>
    >> Using  NSURLConnection I always get some answer - either image data or a lengthy string like:
    >>
    >> HTML 4.0 Transitional//EN">
    >> <html>
    >> <head>
    >> <title>Error 404 - Page not found</title>
    >> </head>
    >> <body bgcolor="#000000">
    >> [...]
    >>
    >>
    >> Is there a way to get just the status (in this case 404) without parsing the data?
    >> I looked at the NSURLConnection delegate methods, but did not find anything which looked appropriate.
    >>
    >> Gerriet.
    >
    > The NSConnectionDelegate protocol has a method that pass you the response:
    >
    > - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    >
    > You can test if the response is of type NSHTTPURLResponse (which should always be the case when you send an HTTP request), and then use the -[NSHTTPURLResponse statusCode] method to get the status.

    Excellent! This is exactly what I was looking for.

    Thank you very much! Merci beaucoup!

    Kind regards,

    Gerriet.
  • On Jun 9, 2013, at 5:54 AM, Jean-Daniel Dupas <devlists...> wrote:

    > The NSConnectionDelegate protocol has a method that pass you the response:
    >
    > - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    >
    > You can test if the response is of type NSHTTPURLResponse (which should always be the case when you send an HTTP request), and then use the -[NSHTTPURLResponse statusCode] method to get the status.

    A much better solution than what I proposed!

    - Koen.
previous month june 2013 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today