首页 > 代码库 > 同步的HTTP请求

同步的HTTP请求

代码:

#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 = http://www.mamicode.com/[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;}

 

同步的HTTP请求