AVaudioRecorder发布请求iOS未发送

时间:2014-09-13 20:00:18

标签: ios post httprequest nsurlconnection avaudiorecorder

我试图通过POST请求向服务器发送avaudiorecorder。但由于某种原因,我无法上传,因此我无法解决。 我首先获取要发送的文件的路径,然后创建一个Post请求方法,其中包含所需的所有标头。服务器响应是没有文件上传,所以我想我对文件的路径做错了但是我没有得到解决方案。 我需要发送一个POST请求,其中包含显示的授权内容,带有多部分表单数据和边界的内容类型,以及一个上传为" uri_file"的文件。和文件。 {uri_file:file} 我尝试发送它,但服务器响应 - NotEnoughData ="你必须提供一个文件&#34 ;; -

...........

-(NSData *)sendTrack{

NSMutableDictionary *dictrvo = [NSMutableDictionary dictionaryWithObjectsAndKeys: audioRecorder.url,@"uri_file", nil ];
NSUserDefaults *prefs= [NSUserDefaults standardUserDefaults];


// get the audio data from main bundle directly into NSData object
NSURL *filePath = [prefs objectForKey:@"Revo1"];
if (filePath == nil) {
    NSLog(@"vacio1");
}
NSData *result =[NSKeyedArchiver archivedDataWithRootObject:dictrvo];
NSData *archivo = [[NSData alloc] initWithContentsOfFile:filePath];
if (result == nil) {
    NSLog(@"vacio");
}

NSData *response= [self createTrackPostRequest:archivo];
NSDictionary *dictionaryResponse = [self getDataFromJson:response];

return nil;

}

//发布请求方法

-(NSData *)createTrackPostRequest:(NSData *)requestData{

NSString *strData = [[NSString alloc]initWithData:requestData encoding:NSUTF8StringEncoding];
// Print out new string
NSLog(@"DATA: %@",strData);
//URL
NSURL *URL = kRevoTrack;
if(URL == nil)
    NSLog(@"estoy vacio");

//REQUEST
NSString *username;
NSString *api_key;
NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:60];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
username = [prefs objectForKey:@"userName"];
api_key = [prefs objectForKey:@"api_key"];
NSString * authorizationcontent = [@"ApiKey " stringByAppendingString:[username stringByAppendingString:[@":" stringByAppendingString:api_key]]];
[request setHTTPMethod:@"POST"];
[request setValue:authorizationcontent forHTTPHeaderField:@"Authorization"];
[request setValue:@"multipart/form-data ; boundary = ---------1239821390213hdjfhsakdasjl123ad" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"form-data; name =\"uri_file\"; file =\"MyAudioMemo.m4a\"\r\n" forHTTPHeaderField:@"Content-Disposition"];
 dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue: [NSString stringWithFormat: @"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:requestData];

NSURLResponse *response = nil;
NSError *error = nil;

NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if(error == nil){

    // REQUEST SENT WITHOUT ERROR
    return result;

}else{

    //ERROR SENDING THE REQUEST

    NSLog(@"ERROR");
}
return nil;
}

有人能帮帮我吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

您只需稍微改变请求的格式。创建多部分请求时,我喜欢先用HTML创建表单。然后,我在启用了Firebug的Firefox中发布示例HTML。然后,我可以分析帖子数据并确保我的请求正文匹配。

-----------------------------173920846362415155847887193 
Content-Disposition: form-data; name="uri_file"; filename="MyAudioMemo.m4a"
Content-Type: audio/x-m4a
<bunch of data>
-----------------------------173920846362415155847887193--

以下是修订后的请求方法。

- (NSData *)createTrackPostRequest:(NSData *)requestData
{
    // Prepare authorization
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *username = [defaults objectForKey:@"userName"];
    NSString *api_key  = [defaults objectForKey:@"api_key"];
    NSString *authorizationcontent = [@"ApiKey " stringByAppendingString:[username stringByAppendingString:[@":" stringByAppendingString:api_key]]];

    // Create the request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:kRevoTrack];
    [request setHTTPMethod:@"POST"];
    [request setValue:authorizationcontent forHTTPHeaderField:@"Authorization"];
    [request setValue:[NSString stringWithFormat: @"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"multipart/form-data; boundary=---------------------------725025452680558686651931680" forHTTPHeaderField:@"Content-Type"];

    // Create the body
    NSMutableData *body = [NSMutableData data];
    [body appendData:[@"\r\n-----------------------------725025452680558686651931680\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"uri_file\"; filename=\"MyAudioMemo.m4a\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: audio/x-m4a\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:requestData];
    [body appendData:[@"\r\n-----------------------------725025452680558686651931680--" dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    // Send the request
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    // Return the result
    if (error == nil) {
        NSLog(@"result: %@", [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]);
        return result; // REQUEST SENT WITHOUT ERROR
    } else {
        NSLog(@"ERROR"); // ERROR SENDING THE REQUEST
    }
    return nil;
}