zoukankan      html  css  js  c++  java
  • ASIHTTPRequest 对GET POST 请求简包

    1.ASIHTTPRequest一个简短的引论

    github下载链接https://github.com/pokeb/asi-http-request


    2.ASIHTTPRequest 对GET和POST请求简单封装

    +(void)requestWithASIURL:(NSString *)urlString parmas:(NSMutableDictionary *)params httpMethod:(NSString *)method completeBlock:(RequestFinishBlock)block{
        //处理GET请求
        if ([[method uppercaseString] isEqualToString:@"GET"]) {
            NSArray *keys=[params allKeys];
            for (int i=0; i<keys.count; i++) {
                NSString *key=[keys objectAtIndex:i];
                NSString *values=[params valueForKey:key];
                urlString= [urlString stringByAppendingFormat:@"&%@=%@",key,values];
            }
            
        }
        NSURL *url=[NSURL URLWithString:urlString];
        ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
        [request setRequestMethod:method];
        [request setTimeOutSeconds:10];
        //处理POST请求
        if ([[method uppercaseString] isEqualToString:@"POST"]) {
            NSArray *keys=[params allKeys];
            for (int i=0; i<keys.count; i++) {
                NSString *key=[keys objectAtIndex:i];
                NSString *value=[params objectForKey:key];
                if ([value isKindOfClass:[UIImage class]]) {
                    //  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test4" ofType:@"gif"];
                    //  NSData*  data=[NSData dataWithContentsOfFile:filePath];
                    NSData *imageData=UIImageJPEGRepresentation(value, 1.0);
                    [request addData:imageData forKey:key];
                }
                [request setPostValue:value forKey:key];
                
            }
        }
    
        [request setCompletionBlock:^{
            NSData *data=[request responseData];
            NSJSONSerialization *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            block(json);
        }];
        [request startAsynchronous];
        
    }
    

    3.调用格式

        NSString* urlString=@"http://192.168.1.101:8080/PengFu/jokController/getPhoneJok";
          NSMutableDictionary *params=[NSMutableDictionary dictionaryWithObject:@"1 " forKey:@"rows"];
        UIImage *image=[UIImage imageNamed:@"test3.gif"];
        [params setObject:image forKey:@"pic"];
        [params setObject:@"test gif image upload" forKey:@"status"];
        
        [DataService requestWithASIURL:urlString1 parmas:params httpMethod:@"POST" completeBlock:^(id result) {
            NSLog(@"%@",result);
        }];
    

    4.注意事项

    下载代码编译报错请先导入网络请求所需的5个库例如以下



    假设使用ARC自己主动内存管理。请在源代码编译后面加-fno-objc-arc,例如以下图


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    HTTP协议详解
    【VC++开发实战】迅雷晒密及批量查询流量程序
    C/C++中指针和引用之相关问题研究
    C++类中拷贝构造函数详解
    构造函数为什么不能是虚函数
    High一下!
    文件搜索神器everything 你不知道的技巧总结
    不要被C++“自动生成”所蒙骗
    对象的传值与返回
    深入浅出Node.js (3)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4867646.html
Copyright © 2011-2022 走看看