drfals1307 2013-03-31 13:09
浏览 45

将文本从label.text更新到服务器并将其保存在文本文件中

i am trying to upload label.text on a server within a text file on the server here is my code. it isnt showing any error but file created on server but no text in it. please help.

NSString *post =[[NSString alloc] initWithFormat:@"VIN: %@",vincode.text];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding
                              allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData
                                                                  length]];
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]
                                        autorelease];
        [request setURL:[NSURL URLWithString:@"Server Detail Here/upload_vin.php"]]; 
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded"
       forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        NSData *urlData = [NSURLConnection sendSynchronousRequest:request
                                                returningResponse:nil error:nil];
        NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
        NSLog(@"VIN is %@",vincode.text);
        NSString *outputdata = [[NSString alloc] initWithData:urlData
                                                     encoding:NSASCIIStringEncoding];
        NSLog(@"Outputdata is %@",outputdata);
        NSURLResponse *response;
        NSError *error;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if(error==nil)
            NSLog(@"Error is nil");
        else
            NSLog(@"Error is not nil");
        NSLog(@"success!");
PHP file::::::::::::::
    <?php
    $file = "VinCodes.txt";
    #$submitted_vincode = $_GET['VIN'] . " 
";
    $submitted_vincode = @$_POST['VIN'] . " 
";
    if(!empty($submitted_vincode)){
    $Handle = fopen($file, 'a');
        fwrite($Handle, $submitted_vincode); 
        print "Vin Code stored in file."; 
        fclose($Handle);
    }else{
        print "Vin code is missing. Try again";
    }
    ?>
  • 写回答

1条回答 默认 最新

  • douguai7291 2013-03-31 15:33
    关注

    Way to much code, here is what I do, (note I upload async):

    NSURL *url = [NSURL URLWithString: @"http://..."];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [NSURLConnection sendAsynchronousRequest:request ...
    

    BTW, are you sure you want NSASCIIStringEncoding and not NSUTF8StringEncoding?

    When in doubt I use Charles Web Proxy (free for 30 days) to see exactly what is sent and the response.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型