1

我正在使用NSMutableURLRequest将数据发送到我的 PHP 服务器。这就是我创建请求的方式:

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[@"whatever" objectForKey:@"message"]] dataUsingEncoding:NSUTF8StringEncoding]];  //converting strings to NSData
[body appendData:[textParams objectForKey:key]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

我的问题是,服务器接收到的数据是什么格式的?IE $_POST['message'],那会是二进制,十六进制,base64,常规字符串吗?

4

1 回答 1

1

“消息”值将作为字符串传递。在您的 PHP 中,您可以执行以下操作:

$message = $_POST['message'];

尽管您的代码有问题。每个要发布的文本值都需要以下行:

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"message"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *text = ... // the text value to post for "message"
[body appendData:[text dataUsingEncoding:NSUTF8StringEncoding];  //converting strings to NSData
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

您试图调用objectForKey:字符串文字。你不能那样做。另外,您正在添加textParams. 我不知道那应该是什么,但是您发布的每个值都需要上述行。也许这些参数需要成为text.

于 2013-03-21T16:21:29.167 回答