28

iPhone アプリで HTTP ステータス コードを確認して評価する必要があります。サイトに正常に接続する NSURLRequest オブジェクトと NSURLConnection があります。

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}

ここでこのコードを取得しました: https://web.archive.org/web/20100908130503/http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

しかし、(私が見る限り) HTTP ステータス コードへのアクセスについては何も述べていません。

サイトへの接続を作成し、その接続の HTTP ステータス コードを確認する方法は?

4

1 に答える 1

76

これは私がそれを行う方法です:

    #pragma mark -
    #pragma mark NSURLConnection Delegate Methods
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
         NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
         int responseStatusCode = [httpResponse statusCode];
    }

しかし、私は非同期 NSURLConnection を使用しているため、これは役に立たないかもしれません。しかし、とにかくそうなることを願っています!

于 2009-05-27T20:48:12.810 に答える