代码:
#import <Foundation/Foundation.h> void request(NSString *urlString) { NSLog(@"BEGIN"); // make request object NSURL *url = [[NSURL alloc]initWithString:urlString]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; [request setHTTPMethod:@"GET"]; [request setTimeoutInterval:10]; // send request NSError *error = nil; NSHTTPURLResponse *urlResponse = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { NSString *responseText = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"OK"); } NSLog(@"END"); } int main(int argc, const char * argv[]) { @autoreleasepool { request(@"http://www.code-style.com"); } return 0; }
时间: 2024-10-06 23:23:55