我正在开发一个将数据发送到Web服务的应用程序。传输的数据使用带有POST方法的https消息。
在服务器端,我用PHP编写了从POST消息中获取数据的代码(只要数据是字符串),
通过使用以下技术。
$servercall = $_POST['servercall'];
我现在发现的问题是传输多部分/表单数据时如何读取数据。
在Objective-C中,我按如下方式构造http消息。
NSURL *url = [NSURL URLWithString:@"<ADDRESS>"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"gc0p4Jq0M2Yt08jU534c0p";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", @"teacherimage"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:Array.picture]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", postSTRING] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
如何在PHP的http消息中阅读照片和文本字符串?我什至不知道我使用Google的技术。
如果有什么不同,所有数据都将保存在SQL数据库中。
谢谢
我不知道Objective-C,但是php具有$ _FILES数组,您可以在其中通过表单查找文件。抱歉,如果我的答案没有意义,我从没使用过objective-c,也没有打算使用它。尝试使用var_dump($_FILES)
。您不需要定义/声明它,因为它是php超全局变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句