2011-10-12 5 views
9

私はiPhone/iPadからPHPサービスに複数行の投稿を送信しようとしていますが、何らかの理由でPOST Content-Typeがapplication/x-www-form-urlencondedのようです私は、これは実際にWiresharkのPOSTパケットキャプチャの抜粋である)、これはWiresharkのを使用して

を見つけた:

**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 
    [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 

問題は、サーバーで、私はこれからのログインとパスワードの変数を読み込むしようとしているということです私はphpがPOSTリクエストがx-www-form-urlencodedだと思っているので、私はを設定すると、

<?php 
echo("<pre>") 
print_r($_POST) 
echo("</pre>") 
?> 

私はこれを取得:から予想されるように、私はこのWiresharkのトレースを取得

<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> 

:私はこの単純なHTMLフォームを使用して要求を送信した場合ので、

<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> 

これは、明らかに良いではありません形式:

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 

このテキストはから出力されます0 authentication.php

<pre>Array\n 
(\n 
    [login] => [email protected]\n 
    [password] => somepassword\n 
)\n 
</pre> 

にこれは最初に私がやる、見ての通り、私はObjective-Cのとココア

- (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; 
} 

でPOSTリクエストを作り上げてる方法です:

[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"]; 

しかし、以前のWiresharkトレースログでは、POSTパケットはContent-Type:application/x-www-form-urlencodedとして表示されていました。

感謝:)

編集:私はASIHTTPRequestのような外部のフレームワークを避けたいが、方法によって、ASIHTTPRequest開発は、私が見つけることができなかったことから、here

+0

私が考えることができる唯一のこと:あなたは手動でhttpリクエストを書いていますか?それはAPIのためのものではありませんか? – CodeCaster

+0

NSMutableURLRequestは提供された "API"ですが、これを行うためのフレームワークはいくつかありますが、私は外部フレームワークを避けようとしています。 – Goles

答えて

19

[OK]を述べたように、リード開発者によって放棄されています外部ライブラリなしの解決策私は自分のコードとそれをopen sourcedで修正しました。

私は投稿のコード上の問題点は、一定のすべてを削除するライン

[myMedRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n", POSTBoundary] forHTTPHeaderField:@"Content-Type"]; 

上の\ r \ nでした。

これは他の誰かに役立つことを願っています:)

+0

リンクが切れています。あなたはそれを更新できますか? –

+0

が更新されました。どのようにPUT/DELETE/PATCHもサポートしています。 :) – Goles

+0

ありがとう!私はそれを試していただけだった。それは入れ子になった辞書を扱うことができますか?私は、ルートレベルの "エントリ"辞書、次にdescription、journal_id、および入れ子になったレベルの画像からなる画像を投稿しようとしています。 –

関連する問題