【问题标题】:POST with Object parameters - iOS带有对象参数的 POST - iOS
【发布时间】:2014-05-30 09:24:20
【问题描述】:

我有一个 iOS 应用程序,它向包括 Facebook 在内的在线服务发出经过身份验证的 POST 请求。我的问题是我知道如何发布文本,但我不能发布图片。这是我的代码:

 NSData *imageData = UIImagePNGRepresentation(image_selected);
    NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

    // Init the URLRequest
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];

    NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];
                    [request setURL:[NSURL URLWithString:facebook_picture_url]];
                    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                    [request setHTTPBody:imageData];
                    request.HTTPBody = imageData;
                    request.HTTPMethod = @"POST";


                    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
                    NSHTTPURLResponse *response = nil;
                    NSError *error = nil;
                    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                    NSLog(@"The responce:\n\n%@", response);

                    if (connection) {
                        // response data of the request
                    }

如您所见,我可以将“imageData”添加到 HTTPBody,但问题是某些服务需要它作为某种参数。例如:Facebook 希望它是:

source="图像数据...."

此外,如果您查看 URL,您会看到有一个 'message=' 参数。这一切都有效,但我肯定可以将它添加到 HTTPBody 中吗??

我需要使用类似 NSDictionary 的东西吗?

感谢您的宝贵时间, 丹尼尔

【问题讨论】:

    标签: ios image cocoa-touch post nsdata


    【解决方案1】:

    感谢开发者社区的帮助,哈哈……无论如何,如果有人正在阅读本文并有兴趣了解如何将图像发布到 Facebook 等服务,我就是这样做的:

    NSData *imageData = UIImagePNGRepresentation(image_selected);
    
    NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/USER_ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:facebook_picture_url]];
    [request setHTTPMethod:@"POST"];
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".JPG\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"The responce:\n\n%@", responseData);
    
    if (error == nil && response.statusCode == 200) {
        NSLog(@"%li", (long)response.statusCode);
    }
    
    else {
      //Error handling
      NSLog(@"%@", response);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      相关资源
      最近更新 更多