0

我有这段代码可以将一些参数发送到 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");
    }];

结果总是空的。没有从服务器下载任何内容。

我应该检查什么?谢谢。

4

1 回答 1

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");
    }];
于 2012-11-25T11:40:24.540 回答