iOS で HTTPResponse/HTTPRequest ステータス コードを取得しますか?
-
06-09-2019 - |
質問
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
}
しかし、(私が見る限り) 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 を使用しているため、これは役に立たない可能性があります。でも、とにかくそうなることを願っています!
所属していません StackOverflow