从iOS发送多部分POST并在PHP $ _POST中读取参数? [英] Sending multipart POST from iOS and reading parameters in PHP $_POST?
问题描述
我正在尝试从iPhone / iPad向php服务发送多行帖子,问题是出于某种原因,POST Content-Type似乎是application / x-www-form-urlenconded, (我发现这是使用Wireshark)
I'm trying to send a multi-line post from an iPhone/iPad to a php service, the problem is that for some reason, the POST Content-Type seems to be application/x-www-form-urlenconded, ( I found out this using Wireshark )
这实际上是Wireshark POST数据包捕获的片段:
This is actually a snippet of the Wireshark POST packet capture:
**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
问题是在服务器中我试图从这个POST请求中读取登录名和密码变量,但这是不可能的,因为我认为php认为POST请求是 x-www-form-urlencoded
,所以如果我设置 authentication.php
来做:
The problem is that in the server I'm trying to read the login and password variables from this POST request, but it's not possible since I think php thinks the POST request is x-www-form-urlencoded
, so if I setup authentication.php
to do:
<?php
echo("<pre>")
print_r($_POST)
echo("</pre>")
?>
我明白了:
<pre>Array\n
(\n
[--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
\n
)\n
</pre>
这显然不太好,因为如果我使用这个简单的html表单发送请求:
This is clearly not good, since If I send a request using this simple html form:
<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>
我从表格中获得了预期的Wireshark跟踪:
I get this Wireshark trace as expected from a form:
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
此文本输出来自 authentication.php $ c中的
print_r($ _ POST)
$ c>
And this text output from the print_r($_POST)
in authentication.php
<pre>Array\n
(\n
[login] => hello@hello.com\n
[password] => somepassword\n
)\n
</pre>
这是我在Objective-C和Cocoa中制作POST请求的方式
This is the way I'm crafting the POST request in Objective-C and Cocoa
- (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;
}
如你所见,我在开头做:
As you see, in the beginning I do:
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
但在之前的Wireshark跟踪日志中,POST数据包被视为Content-Type:application / x-www-form-urlencoded,也许我做错了什么?
But in the previous Wireshark trace logs, the POST packet was being viewed as Content-Type: application/x-www-form-urlencoded, maybe I'm doing something else wrong ?
谢谢:)
编辑:顺便说一句,ASIHTTPRequest开发我想避免像ASIHTTPRequest这样的外部框架已经被首席开发人员抛弃了这里
I want to avoid external frameworks like ASIHTTPRequest, by the way, ASIHTTPRequest development has been abandoned by the lead developer as stated here
推荐答案
好的,因为我找不到没有外部库的解决方案我修复了我的代码并开源,以防万一有人想要使用它。
Ok, since I couldn't find a solution without external libraries I fixed my code and open sourced it, in case anyone want's to use it.
我发布的代码问题是行上的\\\\ n
The problem on the code I posted was the \r\n on the line
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
删除固定的一切。
希望这有助于其他人:)
Hope this helps someone else :)
这篇关于从iOS发送多部分POST并在PHP $ _POST中读取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!