POST从iPhone到PHP不工作(POST from iPhone to PhP not wor

2019-10-16 16:12发布

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

Answer 1:

见我在这里回答有关如何使用POST上传的图像。 它发布到一个PHP文件的形式。 相同的概念张贴数据,不上传图片,但是这是如何发布使用Objective-C的PHP脚本的工作示例。



Answer 2:

尽量不要设置content-type明确。 避免以下行:

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

如果您使用的是iPhone模拟器,我会强烈建议您使用HTTP调试的代理。 mitmproxy是真棒。 而且非常易于安装Mac OS X上。



Answer 3:

使用下面的代码。 确保后弦的收据是一个关键,将在服务器中使用。 客户端代码

 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";
    }
?>


文章来源: POST from iPhone to PhP not working