1

初めて 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 つの異なる方法を試した後、以前の投稿から修正版を取得しました。

そして、同じ結果のさらに別の試み。

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];
4

3 に答える 3

2

POSTを使用して画像をアップロードする方法については、こちらの回答をご覧ください。PHPファイルのフォームに投稿します。データを投稿し、画像をアップロードしない場合の同じ概念ですが、これは、Objective-Cを使用してPHPスクリプトに投稿する方法の実用的な例です。

于 2012-05-04T18:32:02.147 に答える
0

content-type明示的に設定しないでください。次の行は避けてください。

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

iPhoneシミュレーターを使用している場合は、HTTPデバッグの目的でPROXYを使用することを強くお勧めします。mitmproxyすごい。また、 MacOSXでのセットアップは非常に簡単です。

于 2012-05-04T18:31:42.107 に答える
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スクリプト。

検証.php

<?php
 if(_POST)
    {
        if($_POST['receipt'] == 'username')
        {
            echo "post successfull";
         $receipt   = $_POST['key1'];
         echo $receipt;
        }
        else
    {
        echo "not post";
    }
?>
于 2013-03-09T13:01:07.810 に答える