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

在尝试了六种不同的方式后,我从之前的帖子中获取了修改后的版本。

又一次尝试,结果相同。

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 模拟器,我强烈建议您使用 PROXY 进行 HTTP 调试。mitmproxy太棒了。并且非常容易在Mac OS X上设置。

于 2012-05-04T18:31:42.107 回答
0

使用以下代码。确保 post 字符串的收据是一个键并将在服务器中使用。客户端代码

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