HTTP status code 0 - Error Domain=NSURLErrorDomain?

后端 未结 12 2202
予麋鹿
予麋鹿 2020-11-27 14:58

I am working on an iOS project.

In this application, I am downloading images from the server.

Problem:

While downloading images I a

相关标签:
12条回答
  • 2020-11-27 15:29

    The Response was Empty. Most of the case the codes will stats with 1xx, 2xx, 3xx, 4xx, 5xx.

    List of HTTP status codes

    0 讨论(0)
  • 2020-11-27 15:29

    Sometimes Browser respond to http error handler with Error Object, that have status set to 0, even if you can see 404, 401, 500 etc. error status in network.

    This could happen if your Application and API are on different domains - CORS mechanism is applied. According to CORS for each API request browser sends two requests:

    1. preflight OPTIONS request to understand if API allows Actual/Origin request.
    2. when API allows (OPTIOS request respond with status 204 and correct Access-Control-Allow-Origin headers) - browser send next "Actual/Origin request".

    In Application we are handling Error response for "Actual/Origin request", and if "preflight OPTIONS request" failed - browser doesn't give correct HttpError object for http error handler. So to get correct status of http response - be sure to get success preflight OPTIONS request response.

    0 讨论(0)
  • 2020-11-27 15:33

    HTTP response 0 is not standard HTTP response. But it indicates that client could not connect with server and hence forth time out happened.

    0 讨论(0)
  • 2020-11-27 15:34

    This can happen with a 401 http response if using NSURLConnection.

    See NSURLConnection returning error instead of response for 401

    0 讨论(0)
  • 2020-11-27 15:36

    On gate way timeout, status will be zero on your error call back.

    .error( function( data,status,headers,config){
        console.log(status) 
     }
    

    HTTP status codes

    0 讨论(0)
  • 2020-11-27 15:41

    A status code of 0 in an NSHTTPURLResponse object generally means there was no response, and can occur for various reasons. The server will never return a status of 0 as this is not a valid HTTP status code.

    In your case, you are appearing to get a status code of 0 because the request is timing out and 0 is just the default value for the property. The timeout itself could be for various reasons, such as the server simply not responding in time, being blocked by a firewall, or your entire network connection being down. Usually in the case of the latter though the phone is smart enough to know it doesn't have a network connection and will fail immediately. However, it will still fail with an apparent status code of 0.

    Note that in cases where the status code is 0, the real error is captured in the returned NSError object, not the NSHTTPURLResponse.

    HTTP status 408 is pretty uncommon in my experience. I've never encountered one myself. But it is apparently used in cases where the client needs to maintain an active socket connection to the server, and the server is waiting on the client to send more data over the open socket, but it doesn't in a given amount of time and the server ends the connection with a 408 status code, essentially telling the client "you took too long".

    0 讨论(0)
提交回复
热议问题