• post上传文件


    - (BOOL)sendPhotoToTumblr:(NSString *)photo withCaption:(NSString *)caption;
    {
            //get image data from file
            NSData *imageData = [NSData dataWithContentsOfFile:photo];        
            //stop on error
            if (!imageData) return NO;
            
            //Create dictionary of post arguments
            NSArray *keys = [[NSArray alloc] initWithObjects:@"email",@"password",@"type",@"caption",nil];
            NSArray *objects = [[NSArray alloc] initWithObjects:
                            [NSString stringWithFormat:@"%@",CFPreferencesCopyAppValue(CFSTR("TumblrEmail"), kCFPreferencesCurrentApplication)],
                            [NSString stringWithFormat:@"%@",CFPreferencesCopyAppValue(CFSTR("TumblrPassword"), kCFPreferencesCurrentApplication)],
                            @"photo", caption, nil];
            NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
            
            //create tumblr photo post
            NSURLRequest *tumblrPost = [self createTumblrRequest:keysDict withData:imageData];
            
            //send request, return YES if successful
            tumblrConnection = [[NSURLConnection alloc] initWithRequest:tumblrPost delegate:self];
            if (!tumblrConnection) {
                    NSLog(@"Failed to submit request");
                    return NO;
            } else {
                    NSLog(@"Request submitted");
                    receivedData = [[NSMutableData data] retain];
                    return YES;
            }
    }
    
    
    -(NSURLRequest *)createTumblrRequest:(NSDictionary *)postKeys withData:(NSData *)data
    {
            //create the URL POST Request to tumblr
            NSURL *tumblrURL = [NSURL URLWithString:@"http://www.tumblr.com/api/write"];
            NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL];
            [tumblrPost setHTTPMethod:@"POST"];
            
            //Add the header info
            NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
            [tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"];
            
            //create the body
            NSMutableData *postBody = [NSMutableData data];
            [postBody appendData:[[NSString stringWithFormat:@"--%@
    ",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
            //add key values from the NSDictionary object
            NSEnumerator *keys = [postKeys keyEnumerator];
            int i;
            for (i = 0; i < [postKeys count]; i++) {
                    NSString *tempKey = [keys nextObject];
                    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"
    
    ",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"
    --%@
    ",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
            }
    
            //add data field and file data
            [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="data"
    "] dataUsingEncoding:NSUTF8StringEncoding]];
            [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream
    
    "] dataUsingEncoding:NSUTF8StringEncoding]];
            [postBody appendData:[NSData dataWithData:data]];
            [postBody appendData:[[NSString stringWithFormat:@"
    --%@--
    ",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
            //add the body to the post
            [tumblrPost setHTTPBody:postBody];
    
            return tumblrPost;
    }

    1. 设置HttpWebRequest的Content-Type,一定boundary,这里是--ABCD

    Content-Type:multipart/form-data;boundary=--ABCD  
    2. post内容,各部分以--ABCD开头,结尾以--ABCD--结尾
    --ABCD 
    Content-Disposition: form-data; name="title" 
     
    Today 
    --ABCD 
    Content-Disposition: form-data; name="1.txt"; filename="C:1.txt" 
    Content-Type: text/plain 
     
    <这里是1.txt文件的内容> 
    --ABCD--  
     
    注意:换行的地方也是要加 的
  • 相关阅读:
    ACR2010_TNF拮抗剂促进SpA患者椎体侵蚀的修复
    2010年10月第1期(ACR2010专刊AS篇)_中信国健临床通讯目录
    ACR2010_中轴型SpA患者使用TNF拮抗剂治疗后放射学进展与全身炎症消退相关
    ACR2010_小剂量依那西普对达到缓解的AS患者有效
    ACR2010_在高风险人群中应用新版RA分类标准可能发现更多临床前RA
    ACR2010_常规医疗环境下TNF拮抗剂对RA骨侵蚀的修复作用
    ACR2010_HLA B27阳性的极早期AS患者停用TNF拮抗剂后疗效持续: 3个月随机安慰剂对照试验后随访40周
    .net委托与事件 中庸
    筛选法因子之和
    POJ2488 A Knight's Journey 解题报告
  • 原文地址:https://www.cnblogs.com/hereiam/p/4569325.html
Copyright © 2020-2023  润新知