使用SSL和POST与PHP脚本交谈

时间:2012-11-25 10:06:47

标签: iphone ios

我有这个代码将一些参数发送到PHP脚本。理论上,脚本应返回TXT输出,如

header('Content-Type: text/plain');

for ($i = 0; $i < $number; $i++) {

    echo($arrayNumbers[$i] . "\n");
}

输出类似于数字,每行一个,如

13244
23123
23455
... etc.

这是我的iPhone脚本:

    NSString *myRequestString = [NSString stringWithFormat:@"name=%@&phone=%@&address=%@", name, phone, address];

    // encode the URL with % codes
    NSString *escapedString = (NSString *)CFBridgingRelease
        (CFURLCreateStringByAddingPercentEscapes( NULL,
                                                 (__bridge CFStringRef) myRequestString,
                                                 NULL,
                                                 (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                 kCFStringEncodingUTF8));

    NSData *myRequestData = [NSData dataWithBytes:[escapedString UTF8String]
                                            length:[escapedString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                    initWithURL:[NSURL URLWithString: @"https://myserver.com/myScript.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody: myRequestData];

   NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if ([data length] > 0 && error == nil) {
            NSString *content = [NSString stringWithUTF8String:[data bytes]];
            NSLog(@"responseData: %@", content);
        }
        else if ([data length] == 0 && error == nil)
            NSLog(@"empty");
        else 
            NSLog(@"error");
    }];

结果始终为空。没有从服务器下载任何内容。

我应该检查什么?感谢。

1 个答案:

答案 0 :(得分:0)

我已将代码的开头更改为此,现在正在运行。

NSString *myRequestString = [NSString stringWithFormat:@"name=%@&phone=%@&address=%@", name, phone, address];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL URLWithString: @"https://myserver.com/myscript.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[myRequestString dataUsingEncoding:NSUTF8StringEncoding]];

  NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if ([data length] > 0 && error == nil) {
            NSString *content = [NSString stringWithUTF8String:[data bytes]];
            NSLog(@"responseData: %@", content);
        }
        else if ([data length] == 0 && error == nil)
            NSLog(@"empty");
        else 
            NSLog(@"error");
    }];