質問

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 ステータス コードを確認するにはどうすればよいですか?

役に立ちましたか?

解決

これが私のやり方です:

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

ただし、私は非同期 NSURLConnection を使用しているため、これは役に立たない可能性があります。でも、とにかくそうなることを願っています!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top