2012-05-04 9 views
1

POSTを初めて使用してiPhoneからPhPへのPOSTが動作しない

POST情報がiPhoneからPhPスクリプトに送信されていません。私はどこにもエラーはありません。 var_dump($ _ POST)には空が表示されます。ログイン失敗が返されました。次探すか、実行するすべてのアイデアは、私はここで目に見える問題を抱えている:

 NSURL *url = [NSURL URLWithString:link]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

NSString *requestBodyString = [NSString stringWithFormat:@"txtuid=%@&txtpwd=%@", userName , password]; 
NSData *requestBody = [NSData dataWithBytes:[requestBodyString UTF8String]length:[requestBodyString length]]; 

[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:requestBody]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion 

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

それ6さまざまな方法を試した後、私はprevious postから修正版を取りました。

さらに別の試みでも同じ結果が得られます。

NSData *data = [[NSString stringWithFormat: @"txtuid=%@&txtpwd=%@", userName , password] dataUsingEncoding:NSUTF8StringEncoding]; 

// set up request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:link]]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = [NSString stringWithString:@"--------------------------------------------------"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

// body 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:data]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:body]; 
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 
+0

HTTPBody:----------------------------------- ----------------- コンテンツタイプ:テキスト/プレーン txtuid = XXXXX&txtpwd = XXXXX ----------------- ------------------------------------- – anijam

+1

コードに問題がないことを発見しました。サーバーには、すべてのPOST情報を削除していた301リダイレクトがありました。私は最初の試みからコードを使用し、それは正常に働いた。助けてくれた皆様に感謝します。 – anijam

答えて

2

POSTを使用して画像をアップロードする方法については、my answer hereを参照してください。 PHPファイルのフォームに投稿します。データの投稿と画像のアップロードは同じ概念ですが、Objective-Cを使用してPHPスクリプトに投稿する方法の実例です。

+0

私はあなたのことを試みました。上記の投稿されたコード。私は同じ結果を得た。私は何が間違っている/間違っていますか? – anijam

0

明示的にcontent-typeを設定しないでください。

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

iPhoneシミュレータを使用している場合は、HTTPデバッグのためにPROXYを使用することを強くお勧めします。 mitmproxyは素晴らしいです。 Mac OS Xでセットアップがとても簡単です。

0

次のコードを使用してください。ポストストリングのレシートがキーであり、サーバーで使用されることを確認してください。 クライアント側コード

NSString *receipt1 = @"username"; 

    NSString *post =[NSString stringWithFormat:@"receipt=%@",receipt1]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:@"http://localhost:8888/validateaction.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    NSHTTPURLResponse* urlResponse = nil; 
    NSError *error = [[NSError alloc] init]; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response Code: %d", [urlResponse statusCode]); 

    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) 
    { 
     NSLog(@"Response: %@", result); 
    } 

} 

サーバー側PHPスクリプト。

validation.php

<?php 
if(_POST) 
    { 
     if($_POST['receipt'] == 'username') 
     { 
      echo "post successfull"; 
     $receipt = $_POST['key1']; 
     echo $receipt; 
     } 
     else 
    { 
     echo "not post"; 
    } 
?> 
関連する問題