zoukankan      html  css  js  c++  java
  • 同步和异步请求

    同步和异步都是表态方法:

    #pragma mark 异步请求
    - (void)post2 {
        NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/备课.txt"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
       
        // 这个操作队列是用来执行Block的
        NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
         // 请求结束后会回调这个Block
         ^(NSURLResponse *response, NSData *data, NSError *error) {
             NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
             
             NSLog(@"%@", str);
             
             NSLog(@"%@", [response MIMEType]);
        }];
    }

    #pragma mark 同步请求-获取文件的MIMEType
    - (void)getType {
        NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/备课.txt"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        // 发送一个同步请求
        NSURLResponse *response = nil;
        
        // 发送一个同步请求
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        
        NSString *type = [response MIMEType];
        NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
        
        NSLog(@"%@", str);
        NSLog(@"%@", type);
    }

     
     
  • 相关阅读:
    Docker找不到私有nuget服务
    EF中字符串转数字排序
    一个简单的注册页面
    【转】【数据库SQL】SQL查询和替换含有回车,空格,TAB,等
    RGB颜色记录
    javascript中event.keycode
    java基础总结
    面试干货
    jQuery、实例大全
    使用Sql按日期条件查询
  • 原文地址:https://www.cnblogs.com/wangshengl9263/p/3052781.html
Copyright © 2011-2022 走看看