这里我需要使用post方法上传mp3音频,但是写了上传音频的代码,但我不知道如何包含参数。请检查我的代码并帮助我附加参数。
NSString *baseurl = UploadAudio;
// baseurl = [NSString stringWithFormat:@"%@user_id=%@&comments=%@&login_key=%@",baseurl,[[sharedvar userdict] objectForKey:@"user_id"],[self.audiodict objectForKey:@"comments"],[[sharedvar userdict] objectForKey:@"login_key"]];
NSLog(@"baseurl :%@",baseurl);
NSURL *dataURL = [NSURL URLWithString:baseurl];
NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[dataRqst setHTTPMethod:@"POST"];
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// -------------------- ---- Audio Upload Status ---------------------------\\
//pass MediaType file
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; userfile=\"file upload\"; filename=\"uploadaudio.mp3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: audio/mp3\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//*******************load locally store audio file********************//
NSString *audioUrl = [[self audiodict] objectForKey:@"audiopath"];
NSLog(@"audioUrl :%@",audioUrl);
// get the audio data from main bundle directly into NSData object
NSData *audioData;
audioData = [[NSData alloc] initWithContentsOfFile:audioUrl];
// add it to body
[postBody appendData:audioData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [postBody appendData:postData];
// add body to post
[dataRqst setHTTPBody:postBody];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:dataRqst delegate:self];
[connection start]; // connection
我的参数是用户 ID 和会话。任何人都可以帮助我添加这些参数和值