【问题标题】:Server not properly receiving POST request from ASIFormDataRequest in Objective-C服务器未正确接收来自 Objective-C 中 ASIFormDataRequest 的 POST 请求
【发布时间】:2013-06-20 00:31:03
【问题描述】:

我正在尝试在我的 iPhone 应用程序中使用 ASIFormDataRequest 将我刚刚录制的视频以及我可以用来标识视频所属的字符串的字符串发送到服务器。这样做的代码在这里:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
    NSString *urlString=[urlvideo path];
    NSLog(@"urlString=%@",urlString);
    NSString *str = [NSString stringWithFormat:@"http://www.mysite.com/videodata.php"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSData *patientData = [_patientCode dataUsingEncoding:NSUTF8StringEncoding];
    [request setFile:urlString forKey:@"video"];
    [request setData:patientData forKey:@"patientcode"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"responseStatusCode: %i",[request responseStatusCode]);
    NSLog(@"responseString: %@",[request responseString]);
    [picker dismissViewControllerAnimated:YES completion:nil];
}

一切似乎都正常工作,我得到了 200 的状态代码,并且该方法按预期完成。但是,服务器上的 php 文件似乎没有收到任何内容。我将此行添加到我的服务器端 php 代码中:

echo(count($_POST));

这将返回 0,因此 ASIFormDataRequest 似乎没有实际发布任何内容。我觉得可能缺少一些简单的步骤,因为我以前从未使用过 ASIFormDataRequest,但我们将不胜感激。

编辑:我将setData:patientData 更改为setPostValue:_patientCode,现在帖子的一部分已正确发送,所以似乎setPostValue 有效,但setDatasetFile 无效。

【问题讨论】:

  • 我知道这不是你问的,但你真的应该考虑使用 AFNetworking 而不是 ASIHTTPRequest。

标签: objective-c post asiformdatarequest


【解决方案1】:

您应该使用-addData:forKey:-addFile:forKey: 而不是-setData:forKey:-setFile:forKey:

除此之外,使用DEBUG_FORM_DATA_REQUEST 编译时检查调试输出。

您应该在使用-addFile:forKey: 时发送multipart/form-data 请求,在使用-addPostValue:forKey: 时发送application/x-www-form-urlencoded 请求。

您的服务器是否处理 multipart/form-data 请求?

【讨论】:

    【解决方案2】:

    我最终在阅读其他内容时找到了答案,并注意到他们做了一些有趣的事情:我上传的文件只是进入$_FILES 数组,而不是$_POST 数组。我还将[request setData:patientData forKey:@"patientcode"]; 更改为[request setPostValue:_patientCode forKey:@"patientCode"];,然后它出现在我想要的$_POST 数组中,因此我能够在我的php 脚本中获取我发送的字符串和发送的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 2016-11-14
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多