iOS NSURLRequest POST 请求
iOS NSURLRequest POST request
我正在尝试使用以下代码发送包含音频文件和其他一些数据的 POST 请求。服务器编码为 PHP,尽管已发送请求并联系了服务器,但似乎没有 POST 数据可用。下面的代码只是获取一个音频文件和另外 2 个名为 cubeid
和 request_type
的字符串,并将它们作为 POST 变量发送。:
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@smartcube.php",[SMCGlobal apiURL]]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"track.m4a"];
NSURL* soundURL = [NSURL URLWithString:plistPath];
NSData *soundData = [NSData dataWithContentsOfURL:soundURL];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"track.m4a\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: audio/m4a\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:soundData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSDictionary* dic = [SMCGlobal returnUser];
NSString *param1 = @"dhshahds";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cubeid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"request_type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"sendSpeech"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError *connectionError){
NSLog(@"hehe");
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(connectionError){
NSLog([connectionError localizedDescription]);
}else{
NSError *error;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if(error){
NSLog([error localizedDescription]);
}
NSLog(@"haha");
}
}];
当我尝试在服务器上 error_log(json_encode($_POST))
它 returns 一个空字符串。
一些观察:
此代码发送两个请求,一个 XML 请求(没有 XML 正文;大声笑)和一个多部分请求。然后您继续改变要在第二个请求中使用的原始请求。这似乎不太可能是正确的。
我认为您应该删除 application/xml
行的 Content-Type
以及 conn
连接的开头。
我建议不要设置边界,它本身有一堆前导连字符。这不是问题,但如果您在像 Charles 这样的工具中观察原始请求,它只会让您很难解释,因为它不是很明显哪些连字符是多部分分隔符语法的一部分,哪些是边界.
就个人而言,我会使用 [[NSUUID UUID] UUIDString]
作为我的边界。
正如 Wain 所建议的,我会在 Charles 中检查它,看看请求是否格式正确。
我正在尝试使用以下代码发送包含音频文件和其他一些数据的 POST 请求。服务器编码为 PHP,尽管已发送请求并联系了服务器,但似乎没有 POST 数据可用。下面的代码只是获取一个音频文件和另外 2 个名为 cubeid
和 request_type
的字符串,并将它们作为 POST 变量发送。:
// Create the request.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@smartcube.php",[SMCGlobal apiURL]]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"track.m4a"];
NSURL* soundURL = [NSURL URLWithString:plistPath];
NSData *soundData = [NSData dataWithContentsOfURL:soundURL];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"track.m4a\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: audio/m4a\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:soundData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSDictionary* dic = [SMCGlobal returnUser];
NSString *param1 = @"dhshahds";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cubeid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"request_type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"sendSpeech"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError *connectionError){
NSLog(@"hehe");
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(connectionError){
NSLog([connectionError localizedDescription]);
}else{
NSError *error;
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if(error){
NSLog([error localizedDescription]);
}
NSLog(@"haha");
}
}];
当我尝试在服务器上 error_log(json_encode($_POST))
它 returns 一个空字符串。
一些观察:
此代码发送两个请求,一个 XML 请求(没有 XML 正文;大声笑)和一个多部分请求。然后您继续改变要在第二个请求中使用的原始请求。这似乎不太可能是正确的。
我认为您应该删除
application/xml
行的Content-Type
以及conn
连接的开头。我建议不要设置边界,它本身有一堆前导连字符。这不是问题,但如果您在像 Charles 这样的工具中观察原始请求,它只会让您很难解释,因为它不是很明显哪些连字符是多部分分隔符语法的一部分,哪些是边界.
就个人而言,我会使用
[[NSUUID UUID] UUIDString]
作为我的边界。正如 Wain 所建议的,我会在 Charles 中检查它,看看请求是否格式正确。