Senden multipart-POST von iOS und Lesen von Parametern in PHP $_POST?
Ich versuche zu senden, einen multi-line post von einem iPhone/iPad auf einen php-Dienst, das problem ist, dass aus irgendeinem Grund, den POST-Content-Typ zu sein scheint, application/x-www-form-urlenconded, ( ich fand heraus, dies mit Wireshark )
Dies ist tatsächlich ein Ausschnitt aus 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
Das problem ist, dass in dem server, den ich versuche zu Lesen, die login-und Passwort-Variablen, die von dieser POST-Anforderung, aber es ist nicht möglich, da ich denke, dass php denkt, dass die POST-Anforderung ist x-www-form-urlencoded
, also wenn ich setup authentication.php
zu tun:
<?php
echo("<pre>")
print_r($_POST)
echo("</pre>")
?>
Bekomme ich diese:
<pre>Array\n
(\n
[--0xKhTmLbOuNdArY\r\n
Content-Disposition:_form-data;_name] => "login"\r\n
\r\n
[email protected]\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>
Dies ist eindeutig nicht gut ist, da Wenn ich eine Anfrage zu senden, verwenden Sie einfach den html-Formular:
<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>
Bekomme ich das Wireshark-trace, wie erwartet, aus einem Formular:
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
Und diese Textausgabe aus der print_r($_POST)
im authentication.php
<pre>Array\n
(\n
[login] => [email protected]\n
[password] => somepassword\n
)\n
</pre>
Dies ist der Weg, ich bin crafting der POST-Anforderung in Objective-C und 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;
}
Wie Sie sehen, in den Anfang mache ich:
[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"];
Aber in der vorherigen Wireshark-trace-Protokolle, die POST packet angezeigt wurde als Content-Type: application/x-www-form-urlencoded, vielleicht mache ich etwas anderes falsch ?
Dank 🙂
EDIT: ich möchte vermeiden, externe frameworks wie ASIHTTPRequest, durch die Art und Weise, ASIHTTPRequest Entwicklung aufgegeben wurde durch den lead Entwickler wie gesagt hier
- Das einzige was ich mir vorstellen kann: warum sind Sie manuell schreiben Sie eine http-Anforderung? Nicht, dass das, was die API ist für?
- NSMutableURLRequest ist die mitgelieferte "API", es gibt einige frameworks, um dies zu tun, aber ich versuche zu vermeiden, die externen frameworks.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ok, da konnte ich nicht finden, eine Lösung ohne externe Bibliotheken, die ich reparierte meinen code und open-Source es, im Falle jemand will, es zu benutzen.
Dem problem auf den code, den ich geschrieben war die \r\n auf der Linie
Entfernen, die festen alles.
Hoffe, dies hilft jemand anderes 🙂