-2

我想最好使用 NSURLConnection 对 iOS 上的服务器进行同步(但其他类似 Ajax)调用。或者至少在某些情况下是同步的。

我对响应的状态代码感兴趣。如何访问响应 NSURLConnection 的状态码?

欢迎任何示例代码或教程链接;https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLResponse_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLResponse是我最好的现在。

4

2 回答 2

2

对于同步请求,您可以获得这样的响应代码

NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
NSURLRequest *request;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
int code = [responseCode statusCode];
于 2013-10-04T11:46:10.377 回答
2

你可以通过这个使用 nsurlconnection deleate 类的委托函数

(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    int code = [(NSHTTPURLResponse *)response statusCode];
    if (code == 404)
    {
        // website not found
    /    / do your stuff according to your need 
    }
} 

或者在发送同步请求时,您可以检查状态代码,例如

id response2 = nil;

NSError *error1 = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1];

//and then check the  above response status code  like 

int code = [(NSHTTPURLResponse *)response2 statusCode];
    if (code == 404)
    {
        // website not found
    /    / do your stuff according to your need 
    }
于 2013-10-04T12:17:04.843 回答