从iOS发送多部分POST并读取PHP$\u POST中的参数?
我试图将一篇多行文章从iPhone/iPad发送到php服务,问题是出于某种原因,文章内容类型似乎是application/x-www-form-urlengond,(我是通过Wireshark发现的) 这实际上是Wireshark POST数据包捕获的一个片段:从iOS发送多部分POST并读取PHP$\u POST中的参数?,php,objective-c,ios,post,nsurlrequest,Php,Objective C,Ios,Post,Nsurlrequest,我试图将一篇多行文章从iPhone/iPad发送到php服务,问题是出于某种原因,文章内容类型似乎是application/x-www-form-urlengond,(我是通过Wireshark发现的) 这实际上是Wireshark POST数据包捕获的一个片段: **Content-Type: application/x-www-form-urlencoded\r\n** Content-Length: 324\r\n [Content length: 324]
**Content-Type: application/x-www-form-urlencoded\r\n**
Content-Length: 324\r\n
[Content length: 324]
Connection: keep-alive\r\n
\r\n
[Full request URI: http://my.server.com/mobile/tools/authentication]
Line-based text data: application/x-www-form-urlencoded
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="login"\r\n
\r\n
hello@hello.com\r\n
--0xKhTmLbOuNdArY\r\n
Content-Disposition: form-data; name="password"\r\n
\r\n
somepassword\r\n
--0xKhTmLbOuNdArY\r\n
\r\n
--0xKhTmLbOuNdArY--\r\n
<FORM action="http://my.server.com/mobile/tools/authentication.php" method="post">
<P>
<LABEL for="login">E-mail: </LABEL>
<INPUT type="text" name="login"><BR>
<LABEL for="password">pass: </LABEL>
<INPUT type="text" name="password"><BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
问题是,在服务器中,我试图从这个POST请求中读取登录名和密码变量,但这是不可能的,因为我认为php认为POST请求是x-www-form-urlencoded
,因此如果我设置authentication.php
来执行:
<?php
echo("<pre>")
print_r($_POST)
echo("</pre>")
?>
以及从authentication.php中的print\r($\u POST)
输出的文本
如你所见,一开始我是这样做的:
但是在以前的Wireshark跟踪日志中,POST数据包被视为内容类型:application/x-www-form-urlencoded,也许我做了其他错误的事情
谢谢:)
编辑:我想避免像ASIHTTPRequest这样的外部框架,顺便说一句,ASIHTTPRequest开发已经被首席开发人员放弃了,如前所述好的,因为没有外部库我找不到解决方案,我修复了我的代码和它,以防有人想使用它
我发布的代码上的问题是\r\n行上的错误
把它拆了,一切都好了
希望这对其他人有所帮助:)我能想到的唯一一件事是:为什么要手动编写http请求?这不是API的作用吗?NSMutableURLRequest是提供的“API”,有一些框架可以做到这一点,但我试图避免使用外部框架。更新。如何支持放置/删除/修补程序。:)谢谢我只是想试试。它能处理嵌套字典吗?我正在尝试发布一个图像,其中包括根级别的“条目”字典,然后是嵌套级别的描述、日志id和图片。不要这样认为,您可能会在Github上打开一个问题:)
Content-Type: application/x-www-form-urlencoded\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Encoding: gzip,deflate,sdch\r\n
Accept-Language: en-US,en;q=0.8\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n
Cookie: PHPSESSID=tpp10pbrdkkf5dg9qprlamfuu2\r\n
\r\n
[Full request URI: http://mymed2.sophia.inria.fr/mobile/tools/authentication.php]
Line-based text data: application/x-www-form-urlencoded
login=hello%40hello.com&password=somepassword
<pre>Array\n
(\n
[login] => hello@hello.com\n
[password] => somepassword\n
)\n
</pre>
- (NSMutableURLRequest *) POSTRequestWithURL:(NSURL *)url andDataDictionary:(NSDictionary *) dictionary
{
// Create a POST request
NSMutableURLRequest *myMedRequest = [NSMutableURLRequest requestWithURL:url];
[myMedRequest setHTTPMethod:@"POST"];
// Add HTTP header info
// Note: POST boundaries are described here: http://www.vivtek.com/rfc1867.html
// and here http://www.w3.org/TR/html4/interact/forms.html
NSString *POSTBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
// Add HTTP Body
NSMutableData *POSTBody = [NSMutableData data];
[POSTBody appendData:[[NSString stringWithFormat:@"--%@\r\n",POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add Key/Values to the Body
NSEnumerator *enumerator = [dictionary keyEnumerator];
NSString *key;
while ((key = [enumerator nextObject])) {
[POSTBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"%@", [dictionary objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
// Add the closing -- to the POST Form
[POSTBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", POSTBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Add the body to the myMedRequest & return
[myMedRequest setHTTPBody:POSTBody];
return myMedRequest;
}
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];