POSTメッセージを取得しようとしています。私はthis oneのようにそうすることを説明しているいくつかの投稿を見たことがあるが、それでも動作させることはできない。以下は、私のObjective-Cのコードです:NSURLConnectionを使用してiOSのHTTPポストが動作しない
NSString * urlString = @"http://magicthegatheringdatabase.com/test.php";
NSURL *aUrl = [NSURL URLWithString: urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSString *postString = [NSString stringWithFormat:@"tes1=%@&test2=%@",
[@"hello" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[@"world" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData * postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: postBody];
[request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *postLength = [NSString stringWithFormat:@"%d", postBody.length];
[request addValue: postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request
returningResponse: nil
error: &error];
NSLog(@"%p, %@", error, error.localizedDescription);
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Result: %@", returnString);
そして、ここでは私のPHPです:
<?php
print_r($_REQUEST);
print_r($_POST);
print_r($_GET);
echo file_get_contents('php://input');
?>
私は、コードを実行すると、私は次のように記録され得る:
2012-12-19 09:24:09.061 MTG Deck Builder[7921:570f] 0x0, (null) 2012-12-19 09:24:09.062 MTG Deck Builder[7921:570f] Result: Array ( ) Array ( ) Array ( )
私が移動した場合URLに直接添付して?test1=hello&test2=world
を追加してください(POSTの代わりにGETを使用してください)
Array ( [test1] => hello [test2] => world [phpbb3_5e63r_u] => 1 [phpbb3_5e63r_k] => [phpbb3_5e63r_sid] => 7ff37e188aa8e0ab57fae6a52f0d1f7b [__utma] => 86369156.392372889.1349352986.1352901458.1353328580.11 [__utmz] => 86369156.1351935106.8.2.utmcsr=hankinsoft.com|utmccn=(referral)|utmcmd=referral|utmcct=/website/forum/10-mtg-magic-the-gathering-trading-card-game-tcg-da/634-new-forum.html [style_cookie] => null ) Array ( ) Array ( [test1] => hello [test2] => world )
私はPHPがget/requestを正しく記録していることを知っていますが、なぜobjective-cコードによる私のPOSTが機能していないのか分かりません。私は何か愚かなことを見落としていると確信しています。私が紛失していることに関する提案はありますか?
コンテンツタイプとサイズを設定してみてください。 –
nilをerrorに設定するのではなく、もう1つのことをして、NSErrorのインスタンスを渡して、エラーの原因を知ることができます。希望、それはあなたのprobを並べ替えるでしょう。 –
Btw、[RestKit](https://github.com/RestKit/RestKit)について聞きたいですか? – moonwave99