发微博与图片的介绍-----(ios网络编程)

PS:注释的部分为发微博的代码因为和发微博与上传图片代码重复了所以注释了。

//
//  ViewController.m
//  weibo_post
//
//  Created by ios on 16/8/12.
//  Copyright © 2016年 fenglei. All rights reserved.
//

#import "ViewController.h"

#define access_token  //这里没写access_token为了保密如果想自己用的话自己去新浪微博开发平台申请账号 
#define boundary  @"svipsd"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     //***********************发微博*******************
    
////    //创建url
//    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"];
//    
//    //创建request
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    //设置请求方法
//    request.HTTPMethod = @"POST";
//    //设置请求体
//    NSString *requestParams = [NSString stringWithFormat:@"access_token=%@&status=%@",access_token,@"学习了为微博"];
//    
//    request.HTTPBody = [requestParams dataUsingEncoding:NSUTF8StringEncoding];
//    //创建session
//    NSURLSession *session = [NSURLSession sharedSession];
//    
//    //创建一个任务
//    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        
//        NSLog(@"%@",response);
//        
//    }];
//
//
//   //开始任务
//    [task resume];

     //***************发布一条微博并上传图片**************
    //创建一个url
    NSURL *url = [NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"];
    
    //创建一个请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //请求方式
    request.HTTPMethod = @"POST";
    
    //(2)请求头
    //上传任务,必须要添加的字段
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8;boundary=%@",boundary];
    
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
    
    //请求内容
    NSString *status = @"累死了,痛并快乐着,我真的是无语了服务器有毒啊,上传一个图片要这么多步骤。";
    NSString *pic = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"png"];
    
    
    
    
    //请求体
    NSData *bodyData = [self dataWithStatus:status withPicturePath:pic];
    request.HTTPBody = bodyData;
    
    //创建session
    NSURLSession *session = [NSURLSession sharedSession];
    
    //创建任务
    NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSLog(@"%@", response);
        
    }];
    
  
    //开始任务
    [task resume];

    
}
/*
*该方法返回的是请求体的内容
因为涉及到图片上传格式什么的也是醉了
必须严格按照服务器的要求格式写
小编内心是崩溃的。
*/
- (NSData *)dataWithStatus:(NSString *)text
           withPicturePath:(NSString *)path{

    
    //创建一个可变数组进行拼接
    NSMutableString *bodyStr = [NSMutableString string];
    
    //创建一个可变data
    NSMutableData *bodyData = [NSMutableData data];
    //(1)拼接
    [bodyStr appendFormat:@"--%@\r\n",boundary];
    [bodyStr appendFormat:@"Content-Disposition: form-data; name=\"access_token\""];
    [bodyStr appendFormat:@"\r\n\r\n%@\r\n",access_token];
    
    //value2
    [bodyStr appendFormat:@"--%@\r\n",boundary];
    [bodyStr appendFormat:@"Content-Disposition: form-data; name=\"status\""];
    [bodyStr appendFormat:@"\r\n\r\n%@\r\n",text];
    
    [bodyStr appendFormat:@"--%@\r\n",boundary];
    [bodyStr appendFormat:@"Content-disposition: form-data; name=\"pic\"; filename=\"file\""];
    [bodyStr appendFormat:@"\r\nContent-Type: application/octet-stream\r\n\r\n"];
    
    //字符转成data类型
    NSData *startData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [bodyData appendData:startData];
    
    //图片数据
    NSData *imgData = [NSData dataWithContentsOfFile:path];
    [bodyData appendData:imgData];
    
    //结束
    NSString *endStr = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
    NSData *endData = [endStr dataUsingEncoding:NSUTF8StringEncoding];
    [bodyData appendData:endData];
    
  
   
    return bodyData;

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(发微博与图片的介绍-----(ios网络编程))