美文网首页
POST请求之表单上传图片

POST请求之表单上传图片

作者: 于是有了涛 | 来源:发表于2016-12-30 23:40 被阅读381次

    POST请求之表单上传图片

    表单上传:

    表单上传一般是用于上传大型文件给服务器,比如图片、视频、音频等,由于服务器在接收内容的时候需要组合成类似于表格的形式,因此在上传大型文件给服务器的时候就需要用到表单上传的方式上传内容,而在上传大型文件的时候采用POST形式长传

    相关说明:

    在客户端拼接表单格式的时候,首先应声明表单的分割线,然后才能上传,声明分割符在“请求头”中添加,格式如下:


    下面示例中name所对应的是key值
    lng对应的值是key值
    分隔符号是--OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp

    拼接后的格式类似于

    示例:

    通过表单上传格式上传数据给服务器,格式如下:http://m.goufang.com/api/ershou/eshonglist
    --0xKhTmLbOuNdArY
    Content-Disposition: form-data; name="cityid"

    dl
    --0xKhTmLbOuNdArY
    Content-Disposition: form-data; name="page"

    1
    --0xKhTmLbOuNdArY--

    示例代码:

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @protocol ConnectModelDelegate <NSObject>
    
    - (void)connectFinished:(NSData *)data;
    
    @end
    
    @interface ConnectModel : NSObject<NSURLConnectionDataDelegate>
    {
        int classIsa;
    }
    @property (nonatomic, assign) id<ConnectModelDelegate>delegate;
    
    @property (nonatomic, retain) NSMutableData *receiveData;
    - (void)startConnection:(NSString *)str parmaters:(NSDictionary *)parmaters;
    
    + (ConnectModel *)connectWithUrl:(NSString *)url delegate:(id<ConnectModelDelegate>)delegate parmaters:(NSDictionary *)parmaters;
    @property (nonatomic, copy) NSString *requestUrl;
    
    @end
    
    
    #import "ConnectModel.h"
    #import <objc/runtime.h>
    #import <Foundation/NSObjCRuntime.h>
    
    @implementation ConnectModel
    
    + (ConnectModel *)connectWithUrl:(NSString *)url delegate:(id<ConnectModelDelegate>)delegate parmaters:(NSDictionary *)parmaters
    {
            ConnectModel *model = [[ConnectModel alloc] init];
            model.delegate = delegate;
            [model startConnection:url parmaters:parmaters];
            return model;
        
    }
    
    - (void)startConnection:(NSString *)str parmaters:(NSDictionary *)parmaters
    {
        NSString *delegateDescription = [[self.delegate class] description];
        classIsa = (int)objc_getClass([delegateDescription UTF8String]);
        
        // 异步网络连接
        NSString *string = [str stringByAppendingString:[parmaters objectForKey:@"cityid"]];
        self.requestUrl = str;
        
        NSString *bodyStr = @"--0xKhTmLbOuNdArY";
        
        bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
        
        bodyStr = [bodyStr stringByAppendingString:@"Content-Disposition: form-data; name=\"cityid\"\r\n\r\n"];
        bodyStr = [bodyStr stringByAppendingString:[parmaters objectForKey:@"cityid"]];
        bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
        bodyStr = [bodyStr stringByAppendingString:@"--0xKhTmLbOuNdArY\r\n"];
        bodyStr = [bodyStr stringByAppendingString:@"Content-Disposition: form-data; name=\"page\"\r\n\r\n"];
        bodyStr = [bodyStr stringByAppendingString:[parmaters objectForKey:@"page"]];
        bodyStr = [bodyStr stringByAppendingString:@"\r\n"];
        bodyStr = [bodyStr stringByAppendingString:@"--0xKhTmLbOuNdArY--"];
        
        
         NSLog(@"%@", str);
        
        
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        
        [request setHTTPMethod:@"POST"];
        [request setValue:@"multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY" forHTTPHeaderField:@"Content-Type"];
        
        NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
        
        
        
        [request setHTTPBody:data];
        [NSURLConnection connectionWithRequest:request delegate:self];
        
        //    POST http://m.goufang.com/api/loupan/hometuijian HTTP/1.1
        //Host: m.goufang.com
        //    Proxy-Connection: keep-alive
        //    Content-Type: multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY
        //    Content-Length: 162
        //Connection: keep-alive
        //Cookie: ASP.NET_SessionId=khkjhvak2mobhfi34pgdaqc5
        //    User-Agent: Paros/3.2.13
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        self.receiveData = [NSMutableData data];
    }
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [_receiveData appendData:data];
    }
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
        NSString *value = [[NSString alloc] initWithData:_receiveData encoding:enc];
        
        NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"value = %@", value);
        [self.delegate connectFinished:data];
    }
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"error = %@", error);
    }
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
    
    #import "ViewController.h"
    #import "ConnectModel.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    - (IBAction)request:(id)sender {
        
         [ConnectModel connectWithUrl:@"http://m.goufang.com/api/ershou/eshonglist" delegate:nil parmaters:@{@"cityid":@"dl", @"page":@"1"}];
        
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    @end
    

    注意:

    表单上传必须严格按照http的规定格式去拼接url!!

    相关文章

      网友评论

          本文标题:POST请求之表单上传图片

          本文链接:https://www.haomeiwen.com/subject/lwlvvttx.html