zoukankan      html  css  js  c++  java
  • UploadTask-04-网络上传任务

      1 //
      2 //  ViewController.m
      3 //  04-UploadTask
      4 //
      5 #import "ViewController.h"
      6 
      7 #define Boundary @"AaB03x"
      8 
      9 @interface ViewController ()
     10 
     11 @end
     12 
     13 @implementation ViewController
     14 
     15 - (void)viewDidLoad {
     16     [super viewDidLoad];
     17     // Do any additional setup after loading the view, typically from a nib.
     18 }
     19 
     20 - (void)didReceiveMemoryWarning {
     21     [super didReceiveMemoryWarning];
     22     // Dispose of any resources that can be recreated.
     23 }
     24 /*
     25  HTTP请求头:
     26  ....
     27  multipart/form-data; charset=utf-8;boundary=AaB03x
     28  ....
     29  
     30  HTTP请求体:
     31  --AaB03x
     32  Content-Disposition: form-data; name="key1"
     33  
     34  value1
     35  --AaB03x
     36  Content-disposition: form-data; name="key2"
     37  
     38  value2
     39  --AaB03x
     40  Content-disposition: form-data; name="key3"; filename="file"
     41  Content-Type: application/octet-stream
     42  
     43  图片数据...
     44  --AaB03x--
     45 2.00hd363CtKpsnBedca9b3f35tBYiPD
     46  */
     47 - (NSData *)buildBodyData:(NSData *)imgData {
     48     
     49     NSString *accessToken = @"2.00hd363CtKpsnBedca9b3f35tBYiPD";
     50     NSString *statusValue = @"我发美女图片";
     51     
     52     //1.设置status
     53     NSMutableString *bodyStr = [NSMutableString string];
     54     [bodyStr appendFormat:@"--%@
    ", Boundary];
     55     
     56     [bodyStr appendFormat:@"Content-Disposition: form-data; name="status"
    
    "];
     57     [bodyStr appendFormat:@"%@
    ", statusValue];
     58     
     59     //2.设置accessToken
     60     [bodyStr appendFormat:@"--%@
    ", Boundary];
     61     
     62     [bodyStr appendFormat:@"Content-Disposition: form-data; name="access_token"
    
    "];
     63     [bodyStr appendFormat:@"%@
    ", accessToken];
     64     
     65     //3.设置Image
     66     [bodyStr appendFormat:@"--%@
    ", Boundary];
     67     
     68     [bodyStr appendFormat:@"Content-Disposition: form-data; name="pic"; filename="1.jpg"
    Content-Type: application/octet-stream
    
    "];
     69     
     70     NSMutableData *totalData = [NSMutableData data];
     71     NSData *textData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
     72     
     73     //拼前面所有字符串
     74     [totalData appendData:textData];
     75     
     76     //拼图片数据
     77     [totalData appendData:imgData];
     78     
     79     NSString *endBoundary = [NSString stringWithFormat:@"
    --%@--
    ", Boundary];
     80     
     81     [totalData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];
     82     
     83     return totalData;
     84     
     85     
     86 }
     87 
     88 
     89 
     90 - (IBAction)uploadAction:(id)sender {
     91     
     92     NSURL *url = [NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"];
     93     
     94     //构造request
     95     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
     96     request.HTTPMethod = @"POST";
     97     request.URL = url;
     98     request.timeoutInterval = 60;
     99     
    100     //设置请求头
    101     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8;boundary=%@", Boundary];
    102     [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
    103     
    104     //图片数据
    105     NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    106     NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
    107     
    108     NSData *uploadData = [self buildBodyData:imgData];
    109     
    110     NSURLSession *session = [NSURLSession sharedSession];
    111     
    112     NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:uploadData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    113         
    114         NSLog(@"response:%@", response);
    115         
    116         
    117         
    118     }];
    119     
    120     [uploadTask resume];
    121     
    122     
    123     
    124 }
    125 @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    Python 中的 None 与真假
    AVR第5课:蜂鸣器
    Solr使用入门指南
    EJB究竟是什么,真的那么神奇吗??
    Android 各个版本号WebView
    android SQLite 使用实例
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    腾讯QQ企业邮箱POP3/SMTP设置
    【LeetCode】Substring with Concatenation of All Words
    PreferenceFragment 使用 小结
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5433079.html
Copyright © 2011-2022 走看看