0

PHPスクリプトにいくつかのパラメーターを送信するためのこのコードがあります。理論的には、スクリプトは次のようなTXT出力を返す必要があります。

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

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

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

出力は数字のようなもので、1行に1つずつ

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 に答える