【发布时间】:2014-12-08 17:11:07
【问题描述】:
我必须使用 HTTP 方法将 mp3 文件上传到 SoundCloud。我没有收到任何错误,但该文件没有出现在 SoundCloud 上。
我正在使用multipart/form-data request
这是我目前的处理方式:(我已经完成了连接、授权并且我知道我的用户 ID)。
NSString *url=[NSString stringWithFormat:@"http://api.soundcloud.com/tracks.json?client_id=%@&user_id=%@&title=%@&tag_list=%@&sharing=%@&oauth_token=%@",SOUNDCLOUD_CLIENT_ID, inputData[@"user_id"],inputData[@"title"],inputData[@"tag_list”],inputData[@"sharing”], ACCESS_TOKEN];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
request.HTTPMethod = @“POST”;
NSString *boundary= @“SoundCloud”;
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\n--%@\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\”asset_data\"; filename=\"%@\"\n”,@“my file name.mp3"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: audio/mpeg\n\n” dataUsingEncoding:NSUTF8StringEncoding];
NSData *audioData=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"untitled" ofType:@"mp3”]];
[body appendData:[NSData dataWithData:audioData]];
[body appendData:[[NSString stringWithFormat:@"\n--%@", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = body;
[request setValue:[NSString stringWithFormat:@"%lud",(unsigned long)[body length]] forHTTPHeaderField:@"Content-Length”];
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@,boundary] forHTTPHeaderField:@"content-Type"];
request.timeoutInterval = 30;
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];
.mp3 文件实际上是我搜索的位置,因此“audioData”具有正确的内容。
我收到 200 HTTP 响应代码:
NSHTTPURLResponse:{ URL: https://api.soundcloud.com/tracks.json?client_id=[...]&user_id=[...]&title=dgd&tag_list=wgn&sharing=public&oauth_token=[...] } { status code: 200, headers {
"Access-Control-Allow-Headers" = "Accept, Authorization, Content-Type, Origin";
"Access-Control-Allow-Methods" = "GET, PUT, POST, DELETE";
"Access-Control-Allow-Origin" = "*";
"Access-Control-Expose-Headers" = Date;
"Cache-Control" = "private, max-age=0, must-revalidate";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 08 Dec 2014 15:58:57 GMT";
Etag = "\"66cc86baea454001c9572594b9caeea0\"";
Server = "am/2";
"Set-Cookie" = "_session_auth_key=\"LdrvaLfDKfaGihR714EaihOotlo=\"";
"Transfer-Encoding" = Identity;
} }
然后,响应 json 是一个数组,其中包含我帐户中所有曲目的列表。没有错误,但没有上传文件。模拟器和设备得到相同的响应。 欢迎任何建议。
附:我也尝试过像这样更改正文和内容类型的发布请求,但没有工作结果:
NSDictionary *body=@{@"asset_data":[[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"untitled" ofType:@"mp3"]]base64EncodedStringWithOptions:0]};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
request.HTTPBody = bodyData;
[request addValue:@"application/json" forHTTPHeaderField:@"content-Type"];
【问题讨论】:
标签: ios audio file-upload httprequest soundcloud