代码之家  ›  专栏  ›  技术社区  ›  Brandon Schlenker

发送到ImageShack的HTTP邮件

  •  0
  • Brandon Schlenker  · 技术社区  · 15 年前

    我目前正在通过HTTP Post将图像上载到我的服务器。使用下面的代码,一切正常。

    NSString *UDID = md5([UIDevice currentDevice].uniqueIdentifier);
    NSString *filename = [NSString stringWithFormat:@"%@-%@", UDID, [NSDate date]];
    NSString *urlString = @"http://taptation.com/stationary_data/index.php";
    request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:imageData]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];
    
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(returnString);
    

    但是,当我尝试将其转换为使用Image Shacks XML API时,它不会返回任何内容。来自Imageshack的方向如下。

    通过post将以下变量发送到imageshack。U/DIXX.PHP

    文件上传;(图片) xml=“yes”;(指定XML的返回) cookie;(注册码,可选)

    有人知道我该从这里去哪里吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   pokeb    15 年前

    你可能想考虑使用 ASIHTTPRequest ,因为它可以为您构建一个简单的表单数据发布主体,并且可以从磁盘流式传输请求主体,所以在上载大型图像时可以节省内存。

    找到一个快速的谷歌 this ,这似乎表明您应该发布到/upload_api.php,而不是/index.php。

    像这样的事情可能是一个好的开始:

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithUrl:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease];
    [request setFile:@"/path/to/file" forKey:@"fileupload"];
    [request setPostValue:@"yes" forKey:@"xml"];
    [request setPostValue:@"blahblah" forKey:@"cookie"];
    //It looks as though you probably need these too
    [request setPostValue:@"me@somewhere.com" forKey:@"email"];
    [request setPostValue:@"blah" forKey:@"key"];
    [request start];
    if ([request error]) {
        NSLog(@"%@",[request error]);
    } else {
        NSLog([request responseString]); // The xml that got sent back
    }
    

    警告:未测试!

    因为您使用了同步请求,所以我使用了同步请求,但您几乎可以肯定应该使用异步请求(带有asihttpRequest的队列)。

        2
  •  2
  •   max_    14 年前

    花了我一段时间,但你需要使用asihttpRequest!

    - (void)uploadToImageShack {
    NSAutoreleasePool *paul = [[NSAutoreleasePool alloc] init];
    UIImage *tempImage = self.selectedimage;
    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease];
    NSData *imageData = UIImagePNGRepresentation(tempImage);
    [request setFile:imageData withFileName:@"image" andContentType:@"image/png" forKey:@"fileupload"];
    [request setPostValue:@"yes" forKey:@"xml"];
    [request setPostValue:@"3ZQ7C09K708fce677d9cadee04811cfcbdf63361" forKey:@"key"];
    [request setUseCookiePersistence:NO];
    [request startSynchronous];
    NSError *error = [request error];
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else if (!error) {
        if ([request responseString]) {
            parser = [[NSXMLParser alloc] initWithData:[[NSData alloc] initWithData:[request responseData]]];
            [parser setDelegate:self];
            [parser parse];
        }
    }   
    [paul drain];
    }
    
    推荐文章