NSURLSession访问网络数据

1.NSMutableURLRequest的设置

//创建NSMutableURLRequest对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//设置请求类型

[request setHTTPMethod:@"POST"];

//设置超时时间

[request setTimeoutInterval:60];

//设置缓存策略

[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

//设置Base64认证

NSString *authString = [[[NSString stringWithFormat:@"%@:%@",kGlobal.userInfo.sAccount,kGlobal.userInfo.sPassword] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
authString = [NSString stringWithFormat: @"Basic %@", authString];
[request setValue:authString forHTTPHeaderField:@"Authorization"];

//设置POST方法需要传递的参数

NSString *paramStr = [NSString stringWithFormat:@"PostID=%@&Name=%@&Text=%@",self.uuID,kGlobal.userInfo.sApplyName,self.textView.text];
NSData *bodyData = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];

2.创建网络会话

//创建网络会话

NSURLSession *session = [NSURLSession sharedSession];

3.创建网络请求(本文介绍 NSURLSessionDataTask 和 NSURLSessionUploadTask 两种)

//方案一:使用NSURLSessionDataTask请求网络数据

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error == nil) {
            NSLog(@"文字发布成功!");
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;//将response对象强制转换为NSHTTPURLResponse,可以获取HTTP响应报文的头信息,如响应代码200表示请求成功可以用(httpResponse.statusCode获取)
            NSLog(@"返回的Response:%@",httpResponse);
            NSError *error = nil;       //而真正的HTTP响应的Body内容则需要序列化获取
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];//返回一个字典类型数据       //NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];//返回一个JSON数组            //NSArray *array = [NSJSONSerialization JSONObjectWithData:[[dic valueForKey:@"Pictures"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];//[dic valueForKey:@"Pictures"]得到的是第一次解析得到的JSON Array的字符串,需要二次解析成JSON的数组      
if (!error) {
                NSLog(@"返回信息:%@",dic);
            }
        }else{
            NSLog(@"发布失败,代码%@",error);
        }}];

4.发送网络请求

[task resume];

//方案二:使用NSURLSessionUploadTask请求网络数据,NSURLSessionUploadTask 和 NSURLSessionDataTask的不同之处在于NSURLSessionUploadTask更像Web的表单提交,例如模拟Web表单的图片上传可以使用NSURLSessionUploadTask。

NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {if (error == nil) {
            NSLog(@"发布成功!");
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            NSLog(@"statusCode:%lu",httpResponse.statusCode);
            NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

            if (picNum == self.imgsArray.count) {
                [SVProgressHUD showSuccessWithStatus:kTXT(@"IMUploadSuccess")];
                [self dismissViewControllerAnimated:YES completion:nil];
            }

        }else{
            NSLog(@"发布失败,代码%@!",error);
        }}];

上面发送网络请求的方法和方法一的不同之处在于bodyData的构建,方法二的bodyData是模拟HTTP的报文结构来的,使用NSData来拼接HTTP报文。

//bodyData的构建方法

- (NSData *)getBodydataWithImage:(UIImage *)image
{
    //把文件转换为NSData
    NSData *fileData = UIImageJPEGRepresentation(image, 1.0);

    NSString *fileName=[Global getUniqueStrByUUID];

    //1.构造body string
    NSMutableString *bodyString = [[NSMutableString alloc] init];

    //2.拼接body string
    //(1)file_name
    [bodyString appendFormat:@"--%@\r\n",boundry];
    [bodyString appendFormat:@"Content-Disposition: form-data; name=\"FileName\"\r\n"];
    [bodyString appendFormat:@"Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n"];
    [bodyString appendFormat:@"aaa%@.jpg\r\n",fileName];

    //(2)PostID
    [bodyString appendFormat:@"--%@\r\n",boundry];
    [bodyString appendFormat:@"Content-Disposition: form-data; name=\"PostID\"\r\n"];
    [bodyString appendFormat:@"Content-Type: text/plain; charset=\"utf-8\"\r\n\r\n"];
    [bodyString appendFormat:@"%@\r\n",self.uuID];

    //(3)pic
    [bodyString appendFormat:@"--%@\r\n",boundry];
    [bodyString appendFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"%@.jpg\"\r\n",fileName];
    [bodyString appendFormat:@"Content-Type: image/jpeg\r\n\r\n"];
    //[bodyString appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"];

    //3.string --> data
    NSMutableData *bodyData = [NSMutableData data];
    //拼接的过程
    //前面的bodyString, 其他参数
    [bodyData appendData:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
    //图片数据
    [bodyData appendData:fileData];

    //4.结束的分隔线
    NSString *endStr = [NSString stringWithFormat:@"\r\n--%@--\r\n",boundry];
    //拼接到bodyData最后面
    [bodyData appendData:[endStr dataUsingEncoding:NSUTF8StringEncoding]];

    return bodyData;
}

整个bodyData的构建过程原理根据HTTP报文上传图片时,格式用boundory进行分割,但是每次HTTP报文的boundory的值是不一样的,这里为了方便就定义一个固定值,其实固定值也是可以上传成功,亲测有效。HTTP的body体里面把文字和图片内容用boundory和换行符进行分割,分区发送,邮件内容也是类似,所以只要遵循HTTP报文的格式,就可以模拟HTTP报文上传图片。

//boundry的设置

static NSString *boundry = @"----------V2ymHFg03ehbqgZCaKO6jy";//设置边界

//[Global getUniqueStrByUUID]是工具类的方法,用于产生一个GUID,具体的方法明细如下:

+ (NSString *)getUniqueStrByUUID
{
    CFUUIDRef    uuidObj = CFUUIDCreate(nil);//create a new UUID

    //get the string representation of the UUID

    NSString    *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);

    CFRelease(uuidObj);

    return uuidString ;
}

实例代码

时间: 2024-08-08 05:37:02

NSURLSession访问网络数据的相关文章

SWIFT中使用AFNetwroking访问网络数据

AFNetworking 是 iOS 一个使用很方便的第三方网络开发框架,它可以很轻松的从一个URL地址内获取JSON数据. 在使用它时我用到包管理器Cocoapods 不懂的请移步: Cocoapods安装:http://www.cnblogs.com/foxting/p/4520758.html RUBY安装:http://www.cnblogs.com/foxting/p/4520829.html 1.在终端中用CD命令定位到所建项目的根目录,我当前的项目名为Fresh 接着在终端内输入:

使用Python访问网络数据 python network-data 第六章(2)

Welcome 吴铭英 from Using Python to Access Web Data ×Your answer is correct, score saved. Your current grade on this assignment is: 100% Calling a JSON API In this assignment you will write a Python program somewhat similar to http://www.pythonlearn.com

使用Python访问网络数据 python network-data 第五章

Lesson 5--Extracting Data from XML In this assignment you will write a Python program somewhat similar tohttp://www.pythonlearn.com/code/geoxml.py. The program will prompt for a URL, read the XML data from that URL using urllib and then parse and ext

使用Python访问网络数据 python network-data 第六章

question: Extracting Data from JSON The program will prompt for a URL, read the JSON data from that URL using urllib and then parse and extract the comment counts from the JSON data, compute the sum of the numbers in the file. Extracting Data from JS

使用python访问网络上的数据

这两天看完了Course上面的: 使用 Python 访问网络数据 https://www.coursera.org/learn/python-network-data/ 写了一些作业,完成了一些作业.做些学习笔记以做备忘. 1.正则表达式 --- 虽然后面的课程没有怎么用到这个知识点,但是这个技能还是蛮好的. 附上课程中列出来的主要正则表达式的用法: Python Regular Expression Quick Guide ^ Matches the beginning of a line

iOS开发——网络Swift篇&NSURLSession加载数据、下载、上传文件

NSURLSession加载数据.下载.上传文件 NSURLSession类支持三种类型的任务:加载数据.下载和上传.下面通过样例分别进行介绍. 1,使用Data Task加载数据 使用全局的sharedSession()和dataTaskWithRequest方法创建. 1 func sessionLoadData(){ 2 //创建NSURL对象 3 let urlString:String="http://hangge.com" 4 var url:NSURL! = NSURL(

网络数据包分析 网卡Offload

http://blog.nsfocus.net/network-packets-analysis-nic-offload/ 对于网络安全来说,网络传输数据包的捕获和分析是个基础工作,绿盟科技研究员在日常工作中,经常会捕获到一些大小远大于MTU值的数据包,经过分析这些大包的特性,发现和网卡的offload特性有关,本文对网卡Offload技术做简要描述. 文章目录 网络分片技术 网卡offload机制 发送模式 接收模式 网卡offload模式的设置 Linux windows 网卡Offload

Python黑客编程基础3网络数据监听和过滤

Python黑客编程3网络数据监听和过滤 课程的实验环境如下: •      操作系统:kali Linux 2.0 •      编程工具:Wing IDE •      Python版本:2.7.9 •      涉及到的主要python模块:pypcap,dpkt,scapy,scapy-http 涉及到的几个python网络抓包和分析的模块,dpkt和scapy在kali linux 2.0 中默认已经被安装,如果你的系统中没有需要手动安装一下,下面是软件包安装的简单说明. 在kali下

Android访问网络(可以正常使用)

以下是MainActiviy.java,有必要的注释,里面用到了handler,以及线程,workThread如何更新mainThread才能够更新的内容. package com.wyl.httptest2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.