将视频上传到twitpic

时间:2011-10-10 04:26:07

标签: iphone

-(IBAction)pushUpload{

    NSData *media = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]   pathForResource:@"Movie" ofType:@"m4v"]];

   NSString *urlString = @"http://api.twitpic.com/api/upload";

   NSString *key = @" ";
   NSString *message =messagetext.text;


NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 NSString *username = [prefs stringForKey:@"keyToLookupString"];
NSString *password = password.text;
NSUserDefaults *prefs2 = [NSUserDefaults standardUserDefaults];
[prefs2 setObject: password forKey:@"keyToLookupString2"];

// username part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// password part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// key part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[key dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


// message part
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//media part
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media\"; filename=\".m4v\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:media]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setHTTPBody:body];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"response is:%@",returnString);

}

`我正在尝试将视频文件上传到twit pic的指定网址,这有助于用户将他们的视频上传到Twitter。当我按下按钮视频文件应上传到我使用的网址。但当我检查时,得到一个回复​​,你找不到你要求的页面。有人试图上传视频到twit pic。请给我一些示例代码。谢谢。

1 个答案:

答案 0 :(得分:0)

错误与错误的urlString有关。我也没看到你正在使用哪种twitpic-api版本。

twitpic-api-version-2(最新的api-version 应该使用)需要首先通过OAuth和api-key,消息和媒体作为参数进行授权 urlString应如下所示:http://api.twitpic.com/2/upload.xml

twitpic-api-version-1(不是最新的一个:只有那里因为兼容性。将来某个时候会关闭)需要你添加的参数,还需要两个额外的oauth参数,这些参数在你体内缺失。 urlString应如下所示:http://api.twitpic.com/1/upload.xml