码迷,mamicode.com
首页 > 其他好文 > 详细

同步的HTTP请求

时间:2014-10-09 00:09:37      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   sp   div   c   

代码:

#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;
}

 

同步的HTTP请求

标签:style   blog   http   color   io   ar   sp   div   c   

原文地址:http://www.cnblogs.com/code-style/p/4011691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!