post网络请求,支持多层字典参数

下面post请求的参数是没有做多层递归编码的,只是按照公司的规则对第二层进行json化。正常的url参数递归编码,参考AFNetworking里的AFQueryStringFromParameters()函数;

/// post 普通参数
+ (void)postAsynWithURL:(NSURL*)aURL parems:(NSDictionary *)postParems compile:(void(^)(id response, NSData *data, NSError *error))block;
</pre><pre name="code" class="objc">
/// post data、imageData ;imageContentType 填写为image/png、image/webp、image/*之类的,或者不填;imageName 填服务器的image对应的参数名
+ (void)postImageAsynwithURL:(NSURL *)aUrl parems:(NSDictionary *)postKeys withImageData:(NSData *)data ImageName:(NSString *)name ImageContentType:(NSString *)imageContentType compile:(void(^)(id response, NSData *data, NSError *error))block;







+ (void)postAsynWithURL:(NSURL*)aURL parems:(NSDictionary *)postParems compile:(void(^)(id response, NSData *data, NSError *error))block{

    NSString * parameterStr = @"";

    for (NSString * key  in postParems) {

        NSString * content = postParems[key];

        if ([content isKindOfClass:[NSDictionary class]] || [content isKindOfClass:[NSArray class]]) {

            NSData * data = [NSJSONSerialization dataWithJSONObject:content options:0 error:nil];

            content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        }

  //      if (content.length > 0) {

            parameterStr = [NSString stringWithFormat:@"%@&%@=%@",parameterStr,key,content];

  //      }

    }

    if ([parameterStr hasPrefix:@"&"]) {

        parameterStr = [parameterStr substringFromIndex:1];

    }

    NSData * postData = [parameterStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%@",@([postData length])];

//    NSLog(@"url_parameterStr : %@",parameterStr);

    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:aURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];

    [request setHTTPMethod:@"POST"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:postData];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (!connectionError) {

            id resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError];

            block(resultDic,data,connectionError);

        }else{

            block(nil,data,connectionError);

        }

    }];

}
</pre><pre name="code" class="objc">
+ (void)postImageAsynwithURL:(NSURL *)aUrl parems:(NSDictionary *)postKeys withImageData:(NSData *)data ImageName:(NSString *)name ImageContentType:(NSString *)imageContentType compile:(void(^)(id response, NSData *data, NSError *error))block{

    if (imageContentType.length < 1) {

        imageContentType = @"application/octet-stream";

    }

    //Add the header info

    NSString *stringBoundary = @"0xKhTmLbOuNdArY";

    //create the body

    NSMutableData *postBody = [NSMutableData data];

    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //add key values from the NSDictionary object

    NSEnumerator *keys = [postKeys keyEnumerator];

    for (NSInteger i = 0; i < [postKeys count]; i++) {

        NSString *tempKey = [keys nextObject];

        [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];

        NSString * content = [postKeys objectForKey:tempKey];

        

        if ([content isKindOfClass:[NSDictionary class]] || [content isKindOfClass:[NSArray class]]) {

            NSData * data = [NSJSONSerialization dataWithJSONObject:content options:0 error:nil];

            content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        }

        

        [postBody appendData:[[NSString stringWithFormat:@"%@",content] dataUsingEncoding:NSUTF8StringEncoding]];

        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    }

    //add data field and file data

    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"imageName.png\"\r\n",name] dataUsingEncoding:NSUTF8StringEncoding]];

    [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n",imageContentType] dataUsingEncoding:NSUTF8StringEncoding]];

    [postBody appendData:[NSData dataWithData:data]];

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    

    

    NSString *postLength = [NSString stringWithFormat:@"%@",@([postBody length])];

    NSMutableURLRequest *tumblrPost = [[NSMutableURLRequest alloc]initWithURL:aUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];

    [tumblrPost setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];

    [tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"];

    //add the body to the post

    [tumblrPost setHTTPBody:postBody];

    [tumblrPost setValue:postLength forHTTPHeaderField:@"Content-Length"];



    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    [NSURLConnection sendAsynchronousRequest:tumblrPost queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (!connectionError) {

            id resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError];

            block(resultDic,data,connectionError);

        }else{

            block(nil,data,connectionError);

        }

    }];

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值