【发布时间】:2026-01-06 01:55:02
【问题描述】:
我正在尝试将多行帖子从 iPhone/iPad 发送到 php 服务,问题是由于某种原因,POST Content-Type 似乎是 application/x-www-form-urlenconded, (我使用 Wireshark 发现了这一点)
这其实是Wireshark POST抓包的sn-p:
**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要做的事:
<?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 表单发送请求:
<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 跟踪:
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中的print_r($_POST)输出
<pre>Array\n
(\n
[login] => hello@hello.com\n
[password] => somepassword\n
)\n
</pre>
这就是我在 Objective-C 和 Cocoa 中制作 POST 请求的方式
- (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"];
但是在之前的 Wireshark 跟踪日志中,POST 数据包被视为 Content-Type: application/x-www-form-urlencoded,也许我做错了什么?
谢谢:)
编辑:我想避免像 ASIHTTPRequest 这样的外部框架,顺便说一下,正如 here 所述,首席开发人员已经放弃了 ASIHTTPRequest 开发。
【问题讨论】:
-
我唯一能想到的:你为什么要手动写一个http请求?这不就是 API 的用途吗?
-
NSMutableURLRequest 是提供的“API”,有一些框架可以做到这一点,但我试图避免使用外部框架。
标签: php objective-c ios post nsurlrequest