温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用post方法上传文件的两种做法

发布时间:2020-07-24 16:10:37 来源:网络 阅读:1063 作者:wcrane 栏目:开发技术


项目需要使用HTTP协议中的POST方法上传文件,稍微总结了一下,将过程贴出来,方便以后参考。有两种方法,第一是使用NSMutableURLRequest完全从零开始设置,可以加深对HTTP协议的理解;第二种是直接使用别人封装好的代码,如AFNetworking。

方法一,从0开始文件上传
NSURL *url = [NSURL URLWithString:@"http://yo.diveinedu.com/upload.php"];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];request.HTTPMethod = @"POST";//0. 设置分隔符NSString *boundary = @"a1b2c3d4e5f6";[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];NSLog(@"%@", request.allHTTPHeaderFields);NSMutableData *httpBody = [NSMutableData data];//1. 开始的分隔符[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];//2. 设置内容: 标签名,文件名等[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"%@\"\r\n", @"mask0.png"] dataUsingEncoding:NSUTF8StringEncoding]];NSString *path = [[NSBundle mainBundle] pathForResource:@"mask0" ofType:@"png"];NSLog(@"%@", [self typeForPath:path]);//3. 设置内容格式[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [self typeForPath:path]] dataUsingEncoding:NSUTF8StringEncoding]];NSString *body = [[NSString alloc] initWithData:httpBody encoding:NSUTF8StringEncoding];NSLog(@"%@", body);//4. 添加真正的内容NSData *data = [NSData dataWithContentsOfFile:path];[httpBody appendData:data];//5. 添加结束边界[httpBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];//6. 设置http bodyrequest.HTTPBody = httpBody;//7. 发送请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);}];

重点:

  1. 注意协议中需要使用回车换行(\r\n)的位置,HTTP是一个基于文本行的协议,通过回车换行来控制格式。

  2. 注意分隔符(Boundary)的设置,尤其是(--)的增加。

设置后的内容应该是下面的样子(为了看得更清楚,将回车换行用文字表示出来了):

POST /upload.php HTTP/1.1\r\nHost: yo.diveinedu.com\r\nContent-Type: multipart/form-data; boundary=abcdefghigk\r\n\r\nContent-Disposition: form-data; name="fileToUpload"; filename="mask0.png"\r\nContent-Type: p_w_picpath/png\r\n\r\n--abcdefghigk\r\n图片数据\r\n--abcdefghigk--\r\n
方法二,使用AFNetworking中的方法
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];manager.responseSerializer = [AFHTTPResponseSerializer serializer];[manager POST:@"http://yo.diveinedu.com/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //1. 方法一//        NSError *error;//        if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"fileToUpload" fileName:[path lastPathComponent] mimeType:@"p_w_picpath/png" error:&error]) {//            NSLog(@"error appending part: %@", error);//        }

    //2. 方法二
    NSData *data = [NSData dataWithContentsOfFile:path];
    [formData appendPartWithFileData:data name:@"fileToUpload" fileName:[path lastPathComponent] mimeType:@"p_w_picpath/png"];} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSData *data = (NSData *)responseObject;
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);}];

进行网络编程的时候,可以使用Wireshark之类的抓包工具辅助调试。可以很明确的看到发送或接收的数据是否正确。

最后祝大家儿童节快乐~~~

http://io.diveinedu.com

http://www.diveinedu.com

http://bbs.diveinedu.com

https://github.com/DiveinEdu-CN


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI